CALL ACTION FROM JS: HOW TO SEND PARAMETERS OF DIFFERENT DATA TYPES
This blog is all about educating the reader on how to call a custom action (which accepts input parameters and delivers output parameters) from java script. In addition, the most arduous part in this entire task is how should I send parameters of different data types like date time, money, entity reference etc?
Firstly, install CRM Rest API Builder solution which can be used to generate REST API code in order to invoke a custom action. For your reference, I am providing the code:
_opportunityId = Xrm.Page.data.entity.getId();
data = { };
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/opportunities(" + _opportunityId.replace("{", "").replace("}", "") + ")/Microsoft.Dynamics.CRM.new_CUSTOMACTIONNAME", false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204) {
//Success
} else {
//Log Error
}
}
};
req.send(JSON.stringify(data));
If your action is not global, then above code can be used except that you replace logical name of your custom action in the URL part of above code. Now come the part of how to send input parameters to custom action. All the parameters should be included in the data element – data = { Input parameters }
For example, you have built a custom HTML page from where you capture all input data and on button click you pass the data as input parameter to custom action. All the data captured will be in string format and thus needs to be properly type casted before sending to action or else you would get 400 status error – Bad input data from the server.
- Input Parameter – Money, Float, Decimal
data = {“revenue”: parseFloat($(“#renenue_id”).val())};
Here I am using jquery to get the data binded to HTML text box element. Alternatively, you can use document.getElementbyId() to get data. Since from HTML document we always get the output result in string, we need to convert it to float before sending it as input parameter. Similarly use parseInt() if the data type is Integer. Alternative, you can use Xrm.Page.getAttribute(“attribute name”).getValue() to get the data and type caste it to float.
2. Input Parameter – Date Time
data = { “CloseDate”: new Date($(“#closedate_id”).val())};
Here the CloseDate is the actual input parameter name mentioned in action and closedate_id is the HTML class ID of the date picker element.
3. Input Parameter – String
data = { “Description”: $(“#description_id).val())};
This is pretty straight forward because we get the value after querying the HTML element in string and thus we don’t need to perform any convertion.
4. Input Parameter – Entity Reference
data = {“Competitor”: { “competitorid”: GUID OF LOOKUP, “@odata.type”: “Microsoft.Dynamics.CRM.competitor”};
You must provide the GUID of the lookup competitor record and the schema name of the entity in the odata type “competitor”.
5. Input Parameter – Boolean
data = {“ProposalAccepted”: true};
This is very simple as we input either true or false.
6. Input Parameter – Entity
Var Opportunity = {};
Opportunity.name = “Test”;
Opportunity.estrevenue = 35609.00; //decimal
Opportunity.donotphone = true;
Data = {“EntityArg”: Opportunity};
The name of entity should be entered in the action parameter from CRM and all the field values should be entered as above and passed to the data element as EntityArg.

Like
Report
*This post is locked for comments