locked
Direct link to workflow RRS feed

  • Question

  • Is it possible to create a link (in the toolbar for instances) that fires a specific workflow?

    I know how to make toolbar items, but I dont know what to link the toolbar item to.
    Thursday, October 29, 2009 9:43 PM

Answers

  • Hi,
    For this you need two things:
    1. Jscript on the OnLoad event of your form
    2. Modification to your isv.config.

    Here is an example where two buttons exist on a custom entity called aaa_holidayrequest, one fires a 'Request' workflow and the other a 'Cancel' workflow.

    OnLoad script:

    /* Execute Workflow function */
    ExecuteWorkflow = function(entityId, workflowId) {
        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\">" +
        GenerateAuthenticationHeader() +
        "  <soap:Body>" +
        "    <Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
        "      <Request xsi:type=\"ExecuteWorkflowRequest\">" +
        "        <EntityId>" + entityId + "</EntityId>" +
        "        <WorkflowId>" + workflowId + "</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;
        //alert(resultXml.xml);
        return (resultXml.xml);
    }


    ISV config change (note, the GUIDs are the guids of the workflows and there are a couple of extras in there such as an alert and the form closes after pressing the buttons):

    - <Entity name="aaa_holidayrequest">
    - <ToolBar>
    - <Button Icon="/_imgs/ico_24_cal_collapse.gif" JavaScript="ExecuteWorkflow(crmForm.ObjectId, 'C69148DE-1F0F-4A99-906B-CECAD1D4C006'); alert('Your holiday request has been submitted.'); crmForm.SaveAndClose();" Client="Web, Outlook" AvailableOffline="false" ValidForCreate="0" ValidForUpdate="1">
    - <Titles>
      <Title LCID="1033" Text="Request Holiday" />
      </Titles>
    - <ToolTips>
      <ToolTip LCID="1033" Text="Request Holiday" />
      </ToolTips>
      </Button>
    - <Button Icon="/_imgs/ico_24_cal_expand.gif" JavaScript="ExecuteWorkflow(crmForm.ObjectId, 'CA4E5860-2218-4C15-822D-4B66DA221BB2'); alert('Your holiday request has been cancelled.'); crmForm.SaveAndClose();" Client="Web, Outlook" AvailableOffline="false" ValidForCreate="0" ValidForUpdate="1">
    - <Titles>
      <Title LCID="1033" Text="Cancel Holiday" />
      </Titles>
    - <ToolTips>
      <ToolTip LCID="1033" Text="Cancel Holiday" />
      </ToolTips>
      </Button>
      </ToolBar>
      </Entity>


    I hope that does the trick.

    Rob


    CRM4 MBSS
    • Marked as answer by Jay C. _ Monday, November 2, 2009 4:00 PM
    Thursday, October 29, 2009 10:43 PM
    Answerer

All replies

  • Hi,
    For this you need two things:
    1. Jscript on the OnLoad event of your form
    2. Modification to your isv.config.

    Here is an example where two buttons exist on a custom entity called aaa_holidayrequest, one fires a 'Request' workflow and the other a 'Cancel' workflow.

    OnLoad script:

    /* Execute Workflow function */
    ExecuteWorkflow = function(entityId, workflowId) {
        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\">" +
        GenerateAuthenticationHeader() +
        "  <soap:Body>" +
        "    <Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
        "      <Request xsi:type=\"ExecuteWorkflowRequest\">" +
        "        <EntityId>" + entityId + "</EntityId>" +
        "        <WorkflowId>" + workflowId + "</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;
        //alert(resultXml.xml);
        return (resultXml.xml);
    }


    ISV config change (note, the GUIDs are the guids of the workflows and there are a couple of extras in there such as an alert and the form closes after pressing the buttons):

    - <Entity name="aaa_holidayrequest">
    - <ToolBar>
    - <Button Icon="/_imgs/ico_24_cal_collapse.gif" JavaScript="ExecuteWorkflow(crmForm.ObjectId, 'C69148DE-1F0F-4A99-906B-CECAD1D4C006'); alert('Your holiday request has been submitted.'); crmForm.SaveAndClose();" Client="Web, Outlook" AvailableOffline="false" ValidForCreate="0" ValidForUpdate="1">
    - <Titles>
      <Title LCID="1033" Text="Request Holiday" />
      </Titles>
    - <ToolTips>
      <ToolTip LCID="1033" Text="Request Holiday" />
      </ToolTips>
      </Button>
    - <Button Icon="/_imgs/ico_24_cal_expand.gif" JavaScript="ExecuteWorkflow(crmForm.ObjectId, 'CA4E5860-2218-4C15-822D-4B66DA221BB2'); alert('Your holiday request has been cancelled.'); crmForm.SaveAndClose();" Client="Web, Outlook" AvailableOffline="false" ValidForCreate="0" ValidForUpdate="1">
    - <Titles>
      <Title LCID="1033" Text="Cancel Holiday" />
      </Titles>
    - <ToolTips>
      <ToolTip LCID="1033" Text="Cancel Holiday" />
      </ToolTips>
      </Button>
      </ToolBar>
      </Entity>


    I hope that does the trick.

    Rob


    CRM4 MBSS
    • Marked as answer by Jay C. _ Monday, November 2, 2009 4:00 PM
    Thursday, October 29, 2009 10:43 PM
    Answerer
  • THanks for the detailed response, I will give it a shot.
    Thursday, October 29, 2009 10:58 PM
  • ok, so I got it all setup. I entered the onload commands and published the form, I modified the ISV, and I changed the code to reflect teh correct guid for hte workflow.  It appears that the workflow is not being submitted though.  I do not see it appear in System Jobs like I would if I manually launched the workflow from the 'Run Workflow...' button.
    Friday, October 30, 2009 1:23 AM
  • I enabled the result.xml alert you had and I receive this message:
    <faultstring> Server was unable to Process Request</faultstring>

    <error>
    <code>0x80040216</code>
    <desciption> An unexpected error occured</descirption>
    <type>Platform</type>
    </error>
    Friday, October 30, 2009 5:43 PM
  • Hi,
    The above script works fine for me. What is your workflow designed to do? I assume it works fine when the manual 'run workflow' button is pressed?
    Try turning on platform tracing to see if there are further clues to the error. If you don't already have it, try using this platform tracing tool:
    http://blogs.msdn.com/benlec/archive/2008/03/04/crmdiagtool4-for-microsoft-crm-4-0-has-been-released.aspx

    Kind Regards,
    Rob
    CRM4 MBSS
    Friday, October 30, 2009 9:37 PM
    Answerer
  • The workflow is designed to send a simple e-mail, it only has 1 step.  The workflow will run correctly if I use the manual 'run workflow' button.

    Here is the trace:
    at CompositeSoapExtensionExceptionHandler.Handle(Stream to, Stream from, Exception exception)
    at CrmAuthenticationSoapExtensionBase.ProcessMessage(SoapMessage message)
    at SoapMessage.RunExtensions(SoapExtension[] extensions, Boolean throwOnException)
    at SoapServerProtocol.WriteException(Exception e, Stream outputStream)
    at WebServiceHandler.WriteException(Exception e)
    at WebServiceHandler.CoreProcessRequest()
    at SyncSessionlessHandler.ProcessRequest(HttpContext context)
    at CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
    at HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
    at ApplicationStepManager.ResumeSteps(Exception error)
    at HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
    at HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr)
    at HttpRuntime.ProcessRequestNoDemand(HttpWorkerRequest wr)
    at ISAPIRuntime.ProcessRequest(IntPtr ecb, Int32 iWRType)
    >CrmSoapExtension detected non-CrmException - report will be sent to Watson:
    at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()
    at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
    Friday, October 30, 2009 10:18 PM
  • Never mind, it works GREAT!  I copied the GUID wrong.

    Thanks again!
    Monday, November 2, 2009 4:00 PM