title: CRM 2011 - Enable workflow onload of lead entity using javascript?
body: <p>I've checked through the forum and cant seem to get what i need to work.</p>
  
  <p>On the lead entity, when a user creates a new lead a Workflow runs just before to change some lead fields. </p>
  
  <p>I found out this is only possible through javascript and onload in the entity settings.</p>
  
  <p>I've created the Workflow and a JavaScript file and set it to run onload. The Javascript i have used is:</p>
  
  <pre><code>function callworkfow(workflowId, entityId) {
  var request =
  &lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;
  &lt;s:Body&gt;
  &lt;Execute xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services"
  xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
  &lt;request i:type="b:ExecuteWorkflowRequest" xmlns:a="http://schemas.microsoft.com/xrm/2011      /Contracts" xmlns:b="http://schemas.microsoft.com/crm/2011/Contracts"&gt;
   &lt;a:Parameters xmlns:c="http://schemas.datacontract.org/2004/07/System.Collections.Generic"&gt;
   &lt;a:KeyValuePairOfstringanyType&gt;
    &lt;c:key&gt;B3D77337-D5FD-E211-BB05-005056AF0003&lt;/c:key&gt;
    &lt;c:value i:type="d:guid" xmlns:d="http://schemas.microsoft.com/2003/10/Serialization/"&gt;EntityIdValue&lt;/c:value&gt;
        &lt;/a:KeyValuePairOfstringanyType&gt;
        &lt;a:KeyValuePairOfstringanyType&gt;
          &lt;c:key&gt;2EF8C158-182B-444A-A9DF-FF2DC5E44514&lt;/c:key&gt;
          &lt;c:value i:type="d:guid" xmlns:d="http://schemas.microsoft.com/2003/10/Serialization /"&gt;WorkflowIdValue&lt;/c:value&gt;
        &lt;/a:KeyValuePairOfstringanyType&gt;
      &lt;/a:Parameters&gt;
      &lt;a:RequestId i:nil="true" /&gt;
      &lt;a:RequestName&gt;ExecuteWorkflow&lt;/a:RequestName&gt;
    &lt;/request&gt;
      &lt;/Execute&gt;
    &lt;/s:Body&gt;
  &lt;/s:Envelope&gt;
  
  var xhr = new XMLHttpRequest();
  xhr.open('POST', '/XRMServices/2011/Organization.svc/web', true);
  
  xhr.setRequestHeader('Accept', 'application/xml, text/xml, */*');
  xhr.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
  xhr.setRequestHeader('SOAPAction', 'http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute');
  
  xhr.onreadystatechange = function () { alert(xhr.status); };
  xhr.send(request);
  }
  </code></pre>
  
  <p>However this fails to load.</p>
  
  <p>If anyone could help me i would be very grateful. Just need the JavaScript to simply load a Workflow. Oh and with cross platform compatibility.</p>
  
  <hr>
  
  <pre><code>function SetLookupValue(fieldName, id, name, entityType) {
      if (fieldName != null) {
          var lookupValue = new Array();
          lookupValue[0] = new Object();
          lookupValue[0].id = id;
          lookupValue[0].name = name;
          lookupValue[0].entityType = entityType;
          Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
      }
  }
  
  var ExistingCase = curentUserId = Xrm.Page.context.getUserId();
  if (ExistingCase.getValue() != null) {
      var ExistingCaseGUID = ExistingCase.getValue()[0].id;
      var ExistingCaseName = ExistingCase.getValue()[0].name;
      SetLookupValue("new_accountmanager", ExistingCaseGUID, ExistingCaseName, "incident");
  }
  </code></pre>