Call Action in CRM 2015 Easily from JavaScript Library

Paul Nieuwelaar, 12 August 2015

In my previous blog post I introduced a new JavaScript library for calling processes, and I showed the function for calling a workflow. In this blog post I’m going to look at the function for calling an action.

Actions can be very useful when coupled with JavaScript, as it means we can execute complex server side logic, access fields from related records, or execute C# code using custom workflow activities right from within our JavaScript. The problem is, similar to calling workflows, we need to create a massive ugly SOAP request every time we want to call an action. We also need to think about what parameters we’re passing to the action, and what output parameters we’re getting back.

For these reasons, it’s much easier to manage this in a library which we can easily call, and don’t have to worry about remembering the correct format for creating the requests.

To download the JavaScript library, check out the project on CodePlex: https://processjs.codeplex.com/

Call Action

This function calls the specified action and returns the response from the action asynchronously. Useful when needing to run C# code from web resources or command bar buttons when only JavaScript is available.

Parameters: Action Unique Name, Input Parameters (array), Success Callback (function), Error Callback (function), CRM Base URL (not required on forms/views)

Each Input Parameter object should contain key, value, and type. Types are defined by the Process.Type enum. EntityReference values should be an object containing id and entityType.

The Success Callback function should accept one argument which is an array of output parameters, each containing key, and value.

Process.callAction("new_dosomething", [{ key: "Target", type: Process.Type.EntityReference, value: { id: Xrm.Page.data.entity.getId(), entityType: "account" } }, { key: "Input1", type: Process.Type.String, value: "Something" }], function (params) { // Success for (var i = 0; i < params.length; i++) { alert(params[i].key + "=" + params[i].value); } }, function (e) { // Error alert(e); } );

We can see the action here, which takes one string input parameter (plus the Target account reference, since this action isn’t global), and then returns one string output parameter. The output parameter includes the input parameter to prove that the action is receiving the input parameters correctly.

image

When the action is called, we can see the success callback function firing and alerting the output parameter, including the input parameter we passed to the action.

image

To download the JavaScript library, check out the project on CodePlex: https://processjs.codeplex.com/