Monday, December 10, 2018

Trigger Workflow from CRM Portal


In this blog, you will learn how to trigger a workflow dynamically from a portal

If you have a requirement where workflow need to be triggered for more than one record in the background without entity grid button then this will be helpful.

The built in workflow trigger service lives at [portalUrl]/_services/execute-workflow/[portalscopeid]. You can invoke this service by performing a POST to this URL.

Step1 :  First note down the portalscopeid of your portal. If it is a customer portal then GUID value is 7B138792-1090-45B6-9241-8F8D96D8C372.


Step 2: Get the workflow GUID id which needs to be triggered.

Step 3:Copy the below code in Custom Javascript where workflow needs to be called

// JavaScript source code
var recordid = entityrecordid;// Pass the entity record guid for which workflow needs to be triggered                        
var wsid = portalscopeid; //As mentioned in step 1
var wfid = workflowid;// Workflow Guid value                   
if (typeof (wsid) !== 'undefined'&&wsid.length> 0 && typeof (wfid) !== 'undefined'&& wfid.length> 0)
{
var entity = { LogicalName: "entityname", Id: recordid };
var workflow = { LogicalName: "workflow", Id: wfid };
var payload = { entity: entity, workflow: workflow };
varjsonPayload = JSON.stringify(payload);
shell.ajaxSafePost(
        {
        type: "POST",
contentType: "application/json",
            url: "/_services/execute-workflow/" + wsid,
            data: jsonPayload
        }).done(function (msg)
        {
alert("Workflow executed");
        }).fail(function (msg)
        {
            alert("Danger");
        })
}


Step 4: This will trigger the workflow in the CRM.

The built-in services are hidden gems that could really streamline your customisation efforts. There are built-in support facilities that make invoking them relatively easy. You however need to consider your scenario carefully as these are not documented, and therefore may change without notice.

Below is the screenshot for such services which are defined in the RegisterArea method of the Site.Areas.Portal.PortalAreaRegistration class, which is defined in the file \Areas\Portal\PortalAreaRegistration.cs of the Portal project.


This is very useful when any out of box service need to be triggered from portal. ☺️

No comments:

Post a Comment

Show Sitemap Sub areas based on Security Roles

 I came across a requirement where we had to show the Sitemap certain areas only to the Application Admins and hide it from the Users. Yes i...