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;
    }
}


26 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thanks for this help
    Love Apple @ Best Steam Mop

    ReplyDelete
  3. Going to say that I really like to read stuff like this which is shared here.If you want to know which WhatsApp Plus is the best, you will find a WhatsApp Plus here, the best WhatsApp Plus Mod Apk. Read More

    ReplyDelete
  4. amazing codes. thanks for sharing with us. keep it up.
    visit and check

    ReplyDelete
  5. The material and aggregation is excellent and telltale as comfortably.

    gbwhatsapp download for pc

    ReplyDelete
  6. The material and aggregation is excellent and telltale as comfortably.


    notepad plus download

    ReplyDelete
  7. 123movies in our's narrow range, just to identify all the ways how privacy can by abused.

    ReplyDelete
  8. Good Work, i like the way you describe this topic, keep going. i would like to share some useful security links here please go through. gb whatsapp

    ReplyDelete
  9. Thanks for the informative and helpful post, obviously in your blog everything is good.
    Master PC

    ReplyDelete
  10. With Thop Tv you can watch live IPL Matches. IPL (Indian Premier League) is one of the most anticipated sports leagues this year. This cricket tournament is marked as a festival in India. Visit Thoptv IPL Live Match and support your favorite IPL team.

    ReplyDelete
  11. With the pdf mixer crack you will be able to mix PDF pages of 2 or more PDF files, add PDF pages into existing PDF files and save them to a new file. With PDF Mixer, you can easily reorder, copy, rotate, delete, switch, and merge PDF pages in multiple PDF files.

    ReplyDelete
  12. GBWhastapp is full of features. I'm sure you won't choose others if you install it now. Check out the interesting features of gbwhatsapp here.

    ReplyDelete
  13. The best alternative is SpeedCrunch, which is free and open source. demon ps2

    ReplyDelete
  14. Here all features are totally unlocked and unlimited. Mini Militia Hack Version

    ReplyDelete
  15. WhatsApp has been the new communication’s lifeline. If you don’t use the App, you can’t call yourself constructive. gbWhatsApp, on the other hand, adds new features to your favorite app. You also gain more privacy and independence. In addition, the updated edition of WhatsApp comes with over 20 new features. However, there are drawbacks to using the modified version. First and foremost, you risk being barred from WhatsApp Plus, which may result in the loss of valuable data and messages. As a result, you should think twice before using GB WhatsApp Apk GBWhatsApp, as it is not mentioned among the legal apps. We recommend that you consider the advantages and disadvantages before making a decision.

    ReplyDelete
  16. I am really very happy with your article and thanks for sharing it. I’m impressed by the details that you have in this article.

    Social Tips Zone

    bereal profile picture download

    ReplyDelete
  17. There are several features of FM Whastapp Download. If you install it now, I'm confident you won't select any other. View FMwhatsapp's fascinating features here.

    ReplyDelete