Sunday, December 18, 2011

Dynamics CRM 2011 : C# and JScript Code for Executing CRM Workflow


You can use the code below to execute workflows in C# plugin and JScript.
[C# Code]


private void ExecuteWorkflow(Guid prmEntityId, Guid prmWorkflowId)
{
    ExecuteWorkflowRequest wod_ExecuteWorkflowRequest = new ExecuteWorkflowRequest();

    //wod_EntityId contains the Id (GUID value) of record for which the workflow is required to execute
    wod_ExecuteWorkflowRequest.EntityId = new Guid("{7EC1E3A2-6B8C-E011-A88A-E5EF0498D042}");

    //wod_WorkflowId contains the Id (GUID value) of the workflow record (Should have On Demand option checked)
    wod_ExecuteWorkflowRequest.WorkflowId = new Guid("{813B029F-D702-45E1-A6C6-05901413B859}");

    ExecuteWorkflowResponse wod_ExecuteWorkflowResponse = (ExecuteWorkflowResponse)wod_CrmService.Execute(wod_ExecuteWorkflowRequest);
}


[JScript Code]


function RunMyWorkflow() {

    var wod_EntityId;
    var wod_WorkflowId;

    //wod_EntityId contains the Id (GUID value) of record for which the workflow is required to execute
    //Xrm.Page.data.entity.getId(); functions return the current id (GUID value) of record
    wod_EntityId = Xrm.Page.data.entity.getId();

    //wod_WorkflowId contains the Id (GUID value) of the workflow record, make sure that On Demand workflow option is checked
    //You can retrieve the workflow Id (GUID value), open workflow record and copy the URL address.
    //(If you are using CRM application mode then press F11 to make address bar visible)
    //1. In my case URL is: http://localhost:4444/DevelopmentDeployment/sfa/workflow/edit.aspx?id=%7b813B029F-D702-45E1-A6C6-05901413B859%7d
    //2. Decode CRM URL address, open web address http://meyerweb.com/eric/tools/dencoder
    //3. Paste the URL address into the URL Decoder/Encoder text area
    //4. Click two times on Decode button to decode Url address completely.
    //Decoded URL: http://localhost:4444/DevelopmentDeployment/sfa/workflow/edit.aspx?id={813B029F-D702-45E1-A6C6-05901413B859}
    //5. Copy Id= value in my case it is {813B029F-D702-45E1-A6C6-05901413B859} and use in default price list JScript.
  
    wod_WorkflowId = "{813B029F-D702-45E1-A6C6-05901413B859}";

    ExecuteWorkflow(wod_EntityId, wod_WorkflowId);
}

function ExecuteWorkflow(prmEntityId, prmWorkflowId) {

    var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"" +
    " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
    Xrm.Page.context.getAuthenticationHeader() +
    "<soap:Body>" +
    "<Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
    "<Request xsi:type=\"ExecuteWorkflowRequest\">" +
    "<EntityId>" + prmEntityId + "</EntityId>" +
    "<WorkflowId>" + prmWorkflowId + "</WorkflowId>" + "</Request>" +
    "</Execute>" +
    "</soap:Body>" +
    "</soap:Envelope>";

    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);

    xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");

    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);

    xmlHttpRequest.send(xml);

    var resultXml = xmlHttpRequest.responseXML;

    var errorCount = resultXml.selectNodes('//error').length;

    if (errorCount != 0) {
        var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
        alert("Error Message : " + msg);
    }
    else {
        return resultXml;
    }
}


3 comments: