Web API: Executing Custom Action via JavaScript

January 13, 2016

4 comments

The Custom Action is an important tool in Microsoft Dynamics CRM architect belt. Here are some of the Custom Action key features:

  1. It can be called from both client & server side, enabling the SPoI approach (Implement once, consume anywhere)
  2. Supply a flexible, declarative business logic definition mechanism which can be called from other Processes as well as from custom code (unlike Plug-in)
  3. Can be extended with Custom Workflow Activities
  4. 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.

  1. 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.

    Define Custom Action

  2. 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.

    Define calling function
     
    The Action is executed using XMLHttpRequest which is defined to execute synchronously.

    Define Action executing function

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

4 comments

  1. VíctorOctober 6, 2016 ב 4:06 PM

    Hello,

    I have some problems calling a global context action.
    Can yo specify what “Constants.ODATA_ENDPOINT_URI” looks like?

    I wrote it as “/api/data/v8.0/Microsoft.Dynamics.CRM.new_actionname”.
    Do you know what is missing?

    Thank you in advance

    Reply
    1. David MurdockMarch 1, 2017 ב 4:48 PM

      Check Here >https://msdn.microsoft.com/en-us/library/gg334279.aspx

      Search the page for ‘getWebAPIPath()’

      Reply
  2. sivaJanuary 6, 2017 ב 8:03 AM

    In this you have shown
    The Action is executed using XMLHttpRequest which is defined to execute synchronously.

    ##may i know how to call same in “”asynchronous mode””

    Reply
  3. SurmeetJune 9, 2017 ב 5:40 PM

    Its giving 204 in response in which no content is there. How to get the output paramter and status = 200

    Reply