How to Execute Workflow using Java-Script (JS) in MS Dynamic CRM.

Hello guys....Sometime we need to execute workflow using Java-Script.

Prerequisite :
    * Create a workflow which you want to execute. And this workflow must be On-demand.

Note : 
  • Here i am using ActiveX object to retrieve the GUID of workflow. Chrome and Firefox does not support ActiveX object but this object is supported by Internet Explorer on Windows. The below code works only in Internet Explorer. But if you want to execute workflow in all browser then you have to hard-code the GUID of Workflow and remove the getWorkflowId function.

Now copy the below code and paste this in your code.

function Workflow()
{
var workflowName = "WorkFlow Name";
var WF_ID = getWorkflowId(workflowName); // only supported in IE
var entityId = Xrm.Page.data.entity.getId();  // Give Record's GUID on which you want to execute this workflow.
RunWorkflow(WF_ID,entityId);
}

function getWorkflowId(workflowName) {
var name=workflowName;
var serverUrl = Xrm.Page.context.getClientUrl();
var odataSelect = serverUrl + "/xrmservices/2011/OrganizationData.svc/WorkflowSet?$select=WorkflowId&$filter=StateCode/Value eq 1 and ParentWorkflowId/Id eq null and Name eq '" + name +"'";

    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("GET", odataSelect, false);
    xmlHttp.send();

    if (xmlHttp.status == 200) {
        var result = xmlHttp.responseText;
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(result);        
        return xmlDoc.getElementsByTagName("d:WorkflowId")[0].childNodes[0].nodeValue;
    }
}

function RunWorkflow(WF_ID,entityId)
     
    var _return = window.confirm('Do you want to execute workflow.');
    if (_return) {
        var url = Xrm.Page.context.getClientUrl();
        var recordId = entityId;
    var workflowId = WF_ID;
        var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
        url = url + OrgServicePath;
        var request;
        request = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
                      "<s:Body>" +
                        "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                          "<request i:type=\"b:ExecuteWorkflowRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">" +
                            "<a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">" +
                              "<a:KeyValuePairOfstringanyType>" +
                                "<c:key>EntityId</c:key>" +
                                "<c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + recordId + "</c:value>" +
                              "</a:KeyValuePairOfstringanyType>" +
                              "<a:KeyValuePairOfstringanyType>" +
                                "<c:key>WorkflowId</c:key>" +
                                "<c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + workflowId + "</c:value>" +
                              "</a:KeyValuePairOfstringanyType>" +
                            "</a:Parameters>" +
                            "<a:RequestId i:nil=\"true\" />" +
                            "<a:RequestName>ExecuteWorkflow</a:RequestName>" +
                          "</request>" +
                        "</Execute>" +
                      "</s:Body>" +
                    "</s:Envelope>";

        var req = new XMLHttpRequest();
        req.open("POST", url, true)
        // Responses will return XML. It isn't possible to return JSON.
        req.setRequestHeader("Accept", "application/xml, text/xml, */*");
        req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
        req.onreadystatechange = function () { assignResponse(req); };
        req.send(request);
    }
}

function assignResponse(req) {
    if (req.readyState == 4) {
        if (req.status == 200) {
            alert('successfully executed the workflow');
        }
    }
}


Hope this code helps you....

How to Set Up custom Domain of D365 Portal.

In this post, I will explain how to remove *.microsoftcrmportals.com domain and add our custom domain of D365 Portal. 

Prerequisite to Set Up Custom Domian of D365 Portal :

* You should have the Licensed Organization, In Trial organization you don't have any option to set up custom domain.
* Purchase a Custom Domain from any other company like GoDaddy which you want to set up with your portal.
* After purchase Custom Domain, you also need SSL Certificate. This certificate will be issue by the same company from where you have purchased the domain.

You have to perform some steps to issue SSL. Firstly you have to generate .CSR file to issue SSL from company

Step 1: Download the tool DigiCertUtil and Install it in your system or Download it from Here.

Step 2: Now open this tool and click on Create CSR.


Step 3: After click on create CSR, Provide needful information and then click on Generate. Certificate type must be SSL.

Step 4: Now a popup will come and then click on  Save to File.  

Now you have .CSR file to generate the SSL certificate. Give this .CSR to company and then they will issue SSL Certificate.

To Setup Custom Domain of D365 Portal. 

Step 1: Go to Portal Admin Section. Click on Set up custom domains and SSL. After click a pop-up will come

Step 2: Now select the Option Upload a new certificate and then upload the SSL Certificate which you have Issued. And then click on Next button.

Step 3: Now select the Option Add a new host name and Give your domain name which you want to set. And then click on Next button.


Step 4: Now you are on Binding Step. Here you can see the information which you have bound with the portal and then click on Next button.

Step 5: Now you are on Confirmation step. Here you can see your message and then click on finish.



Now you have successfully set up your custom domain of D365 Portal.  

How to perform Registration process through OTP in D365 Portal / ADX Studio Portal.

In D365 Portal / ADX Studio Portal, If you want to perform registration process through OTP then follow the below steps. If you want to perform registration process through E-mail then click Here.

Step 1: Create a workflow which create Invitation record. This workflow will run after creation of Contact.



Step 2: Now write a plugin which is used to generate the OTP code as show in below Snapshot. In this code:

  • We generate the OTP code using Random function.
  • Put this OTP code in Invitation Code field and Update the Invitation. 


Step 3: Register the plugin on creation of Invitation and the execution of plugin will be PostOperation. 

Step 4: Now write a another workflow which is used to Send SMS to User's Phone number. This workflow will execute on change of Invitation code.

Now the OTP comes in your phone Number and Enter the OTP in Portal's Invitation Code.


 Now you are able to register the user through the OTP.