Saturday, 21 May 2016

How to call workflow from javascript in MS CRM

Some time you needs to perform some actions in a record at random time/manually.
For ex: Sending a predefined Manual SMS(which itself would be a custom feature) on  record using a custom button.
In such case traditionally developers tends to create a boolean field and toggle that boolean field using ribbon button using js.
On change of that boolean field workflow is triggered.
BUT it's not necessary to follow the same approach. You can directly call a workflow from javascrip.
Below is the code snippet :
It runs the specified workflow for a particular record asynchronously. Optionally, you can add callback functions which will fire when the workflow has been executed successfully or if it fails.
Parameters: Workflow ID/Guid, Record ID/Guid, Success Callback (function), Error Callback (function), CRM Base URL (not required on forms/views)


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Process.callWorkflow("4AB26754-3F2F-4B1D-9EC7-F8932331567A", 
    Xrm.Page.data.entity.getId(),
    function () {
        alert("Workflow executed successfully");
    },
    function () {
        alert("Error executing workflow");
    });
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Don't forget to import Processjs solution into your CRM instance provided by codeplex to make it work.

Note: To not make the GUID of workflow hard-coded, you can fetch GUID of workflow fromm it's name using Odata/Web code.

No comments:

Post a Comment