CRM 4.0 : 用JavaScript调用Workflow


昨天同事问我,怎样用 JavaScript调用Workflow。同样的问题在CRM Forum里也经常被问,至今没有答案。在CRM 3.0里,Mitch Milam 有篇文章讲到了这个用法,它用到了ExecuteWFProcesRequest 类,但是4.0里这个类被摒弃了(原因大家也知道,4.0里使用WFF)。其实调用Workflow有很多方法,但是如果你想用JavaScript调用,可以这样(函数放在窗体OnLoad里面):


/* the 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;
    
return(resultXml.xml);
}

/* call */
var theWorkflowId = "3FD2DD58-4708-43D7-A21B-F0F90A0AA9F2"//change to your workflow Id
ExecuteWorkflow(crmForm.ObjectId, theWorkflowId);


注意这个类ExcuteWorkflowRequest只支持在线方法,如果你想在窗体上加一个按钮,点击执行这个工作流,可以修改ISV.CONFIG:

<Button Title="Run Workflow" Icon="/_imgs/ico/16_runworkflow.gif" JavaScript="ExecuteWorkflow(crmForm.ObjectId, '3FD2DD58-4708-43D7-A21B-F0F90A0AA9F2');" />

posted @ 2008-07-03 14:48  MicrosoftCRM  阅读(1613)  评论(1编辑  收藏  举报