While using CRM there may be conditions in which user wants to open forms and reports programmatically. For this we can write the scripts which will open the forms and reports.
Open CRM forms using script:
The form can be open using openEntityForm(); method.
Syntax : Xrm.Utility.openEntityForm(name,id,parameter);
Parameters :
Ø name: (Required ) The logical name of an entity.
Ø id: (Optional) The string representation of a unique identifier or the record to open in the form. If not set, a form to create a new record is opened.
Ø parameters: (Optional) A dictionary object that passes extra query string parameters to the form. Invalid query string parameters will cause an error.
Example:
1. To Open Blank new entity record :
Xrm.Utility.openEntityForm(“contact”);
2. To Open Existing CRM Form :
Xrm.Utility.openEntityForm(“contact”,”A85C0252-DF8B-E111-997C-00155D8E4810″);
3. To open Form with default value set using parameter
function OpenNewContact() {
var parameters = {};
//Set the first name of contact to “Scott”
parameters[“firstname”] = “Scott”;
//Set text in the Description field.
parameters[“description”] = “Description for this record”;
//Set the Parent Customer field value to “John”.
parameters[“parentcustomerid”] = “2E862A5A-7D0F-E211-8090-00155D000501”;
parameters[“parentcustomeridname”] = ” John “;
parameters[“parentcustomeridtype”] = “account”;
//Set Do not allow phone to “Do Not Allow”.
parameters[“donotphone”] = “1”;
// Open the window.
Xrm.Utility.openEntityForm(“contact”, null, parameters);
}
Open Report using script:
We can also run the report using script. We just have to pass the url to window.open(); method. Below example shows how to open the report.
Syntax : window.open(url);
url need the following parameters:
Ø action: Two possible values for this parameter are run or filter.
· When run is used, the report will be displayed using the default filters.
· When filter is used, the report will display a filter that the user can edit before clicking the Run Report button to view the report.
Ø helped:(optional) The value should correspond to the report FileName attribute value.
Ø Id :This parameter is the report ReportId attribute value.
Example:
function OpenReport() {
try {
//call function which will return encoded url
var url=getReportURL(filter,AllContacts,’35F560E6-1606-E211-A8FC-00155D000501′);
window.open(url);
}
catch(e){
alert(“OpenReport Error >> “+e.description);
}
}
//function to create and encode url
function getReportURL(action,fileName,id) {
var orgUrl = Xrm.Page.context.getClientUrl();
var reportUrl = orgUrl +
“/crmreports/viewer/viewer.aspx?action=” +
encodeURIComponent(action) +
“&helpID=” +
encodeURIComponent(fileName) +
“&id=%7b” +
encodeURIComponent(id) +
“%7d”;
return reportUrl;
}