Showing posts with label scripts. Show all posts
Showing posts with label scripts. Show all posts

Thursday, December 12, 2013

Pass Custom Parameters to an entity form through a URL in CRM 2013


This blog posted back in 2012, described how to pass custom parameters to an entity in CRM 2011. The other day tried to use the same code to make it in CRM 2013 but unfortunately that would not work. You will be able to pass a single parameter through the URL using the same code as before, but to pass more than one parameters, you need to use the new openEntityForm client API.

In 2011, the parameters would be concatenated to make a string and pass that as the extraqs query string

var extraqs = "Parameter_Source=Hello";
extraqs += "parameter_Source2=8";


//Set features for how the window will appear.
var features = "location=no,menubar=no,status=no,toolbar=no";
// Open the window.
window.open(Xrm.Page.context.getServerUrl() +"/main.aspx?etn=account&pagetype=entityrecord&extraqs=" + encodeURIComponent(extraqs), "_blank", features, false);


Using the new client API to open the record, this would now be presented as follows

var parameters = {};

parameters["myparam_test"] = "1";

parameters["parameter_test"] = "100";

//use the openEntityForm to open the record

Xrm.Utility.openEntityForm("account", null, parameters);

Wednesday, January 30, 2013

Supported ways of accessing URL addressable Forms/Reports Scripts.

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;
  }
 

Wednesday, February 1, 2012

Pass Custom Parameters to an entity form through a URL

In your applications, you may want to pass custom query string parameters to an entity form.
Let me describe the way in which we can define a set of specific parameter names and data types that can be accepted for a specific entity form.

Define Allowed Query String Parameters:
There are two ways to specify which query string parameters will be accepted by the form:
• Edit form properties
• Edit form XML

Edit Form Properties:
When you edit an entity form, on the Home tab in the Form group, click Form Properties. In the Form Properties dialog box, select the Parameters tab. Use this tab to modify the names and data types that the form allows as given in the below screen shot.


The following describes the querystringparameter element attributes, name and type:Name: Each name attribute must contain at least one underscore ('_') character, but the name of the query string parameter cannot begin with an underscore. The name also cannot start with 'crm_'. We strongly recommend that you use the customization prefix of the solution publisher as the naming convention. If you don’t follow the naming conventions the alert message as given in the below screen shot will be shown to the user.


Type: Following types of parameters can be passed to the form. Match the data type values with the parameter values so that invalid data is not passed with the parameter. The following are valid data types:
• Boolean
• DateTime
• Double
• EntityType
• Integer
• Long
• PositiveInteger
• SafeString
• UniqueId
• UnsignedInt

Let me define the parameters for the form and access them through Query string parameters.

- We have created two parameters for the Account Form as given in the below screen shot.


- We have opened the form through the below given script with parameters appended to the URL.
var extraqs = "Parameter_Source=Hello";
extraqs += "parameter_Source2=8";
//Set features for how the window will appear.
var features = "location=no,menubar=no,status=no,toolbar=no";
// Open the window.
window.open(Xrm.Page.context.getServerUrl() +"/main.aspx?etn=account&pagetype=entityrecord&extraqs=" + encodeURIComponent(extraqs), "_blank", features, false);

- After that we have checked it through the query string parameters.
// Get the Query String Parametrs passedthrough the URL
var param=context.getQueryStringParameters();
// Get the Value of the Source through the Key Parameter Name
var source=param["Parameter_Source"];
var source2=param["parameter_Source2"];
// if Source is Undefined i.e. the URL Doesn't contain the key
if (source==undefined){ return; }
else{ // Perform the operation
}

This will be helpful in the scenario suppose if are opening the Form through the different sources such as Silverlight, report etc. and on the form load you want to perform some operation based on the source then you can define parameter for the form and can pass the source name in the extraqs of the URL as mentioned above and can check the source name on the form load and can perform the operation accordingly.

Tuesday, October 18, 2011

Enable Scripts on Bulk Edit form in CRM 2011

CRM 2011 includes the feature to enable script on the bulk edit form. By default the scripts are disabled for bulk edit forms.

To enable the script for Bulk edit forms check the below syntax

<event
active=["0" "1" "true" "false"]
application=["0" "1" "true" "false"]
attribute="String"
BehaviorInBulkEditForm="String"
eventType=["DataEvent" "ControlEvent"]
name="String">
<dependencies />
<Handlers />
<InternalHandlers />
</event>

See below detailed descriptions for the attributes.




Steps to enable the script on bulk edit form:
- Include the entity for which you would like to enable the bulk edit script and export the solution.
- Open the customization.xml
- Add BehaviorInBulkEditForm="Enabled" to the event handlers as shown below


<event name="onload" application="false" active="false" BehaviorInBulkEditForm="Enabled" >
<event name="onsave" application="false" active="false" BehaviorInBulkEditForm="Enabled" >

- Save and Import the solution, publish it.
- Now your script will work on bulk edit form.

This way you can let your users use bulk edit freely with your custom script also executing just fine. Earlier, usually if there was custom business logic to be performed plugins were advised as it would allow the custom business logic to be executed for all records that were edited through Bulk Edit.