The Custom Action is an important tool in Microsoft Dynamics CRM architect belt. Here are some of the Custom Action key features:
- It can be called from both client & server side, enabling the SPoI approach (Implement once, consume anywhere)
- Supply a flexible, declarative business logic definition mechanism which can be called from other Processes as well as from custom code (unlike Plug-in)
- Can be extended with Custom Workflow Activities
- Can receive input parameters and return output parameters in a straight forward manner, similar to an Organization level Web Service
Why would you want to execute a Custom Action (which is a server side mechanism) via JavaScript? Here are some excellent reasons:
- Provide an improved UX, as Custom Action can be executed a-synchronously from entity forms, web resources and Ribbon/Command Bar controls without blocking the user UI
- You can bind server side logic to client side events, e.g. calling a Custom Action from an onChange or onSave event handlers
- Implement a chunky rather than chatty communication with the server, as Custom Action usually wraps multiple operations which otherwise require multiple API commands
You can execute a Custom Action via JavaScript using raw SOAP (not recommended) or better yet, using the Sdk.Soap.js library (still SOAP, but nicely wrapped).
With Microsoft Dynamics CRM 2016 Web API you can now execute a Custom Action in the most simple manner, which is demonstrated in the rest of this post.
You can download an unmanaged Solution containing the sample Action and JavaScript code. This solution is for demonstration purposes only, please do not install on production or other critical environments.
- Define and activate a Custom Action in MSCRM 2016 Organization
My sample Action receives few parameters, creates a Lead record and returns the new record URL.
Note that this Action is defined for a global context. Also note the Action’s unique name and parameters names which are used in the following code. - Write JavaScript code to consume your Action
The handleWebLead function defines the dyn_HandleWebLead Action name and parameters.
It than calls the generic activateCustomAction function to execute the Action and return the output result.
The Action is executed using XMLHttpRequest which is defined to execute synchronously.