Saturday, November 22, 2008

Execute workflow from ISV button

To optimize the user experience some of the workflows that are executed on demand might be put into an ISV button on the entity form.
Here is what to do:


Go to settings/customization
Open the entity and select the form in the forms/view section
Open the forms onload event
Copy the code below and paste it in at the end of what ever might be in the editor window


"/* the function */
ExecuteWorkflow = function(entityId, workflowId)
{
var xml = ''  
''  
'<?xml:namespace prefix = soap /><soap:envelope xsd="\'http://www.w3.org/2001/XMLSchema\'" xsi="\'http://www.w3.org/2001/XMLSchema-instance\'" soap="\'http://schemas.xmlsoap.org/soap/envelope/\'">'  
GenerateAuthenticationHeader()  
' <soap:body>'  
' <execute xmlns="\'http://schemas.microsoft.com/crm/2007/WebServices\'">'  
' <request 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;
return(resultXml.xml);
}"




After save the form and publish the entity
Export the ISV config File and open it in notepad.
For the entyform you want to execute the workflow from, enter the nodes below:


"<entity name="'account'">
<toolbar validforcreate="'0'" validforupdate="'1'">
<Button Icon='/_imgs/ico_16_exportCustomizations.gif'
JavaScript='
var theWorkflowId = '3FD2DD58-4708-43D7-A21B-F0F90A0AA9F2';  
ExecuteWorkflow(crmForm.ObjectId, theWorkflowId); 
window.location.reload();}'>
<titles>
<title lcid="'1033'" text="'Run">
</titles>
<tooltips>
<tooltip lcid="'1033'" text="'Run">
</tooltips>
</button>
</toolbar>
</entity>"




Import the ISV.Config into CRM
Open the form and run your workflow directly from the form.