javascript - 在 CRM 2011 中从 JavaScript 执行工作流

标签 javascript workflow dynamics-crm dynamics-crm-2011

我正在尝试通过功能区按钮为 View 中选择的记录执行工作流。我有一个使用“遗留”服务实现 CRM 4 兼容性的工作示例:

function invokeWorkflow(workflowId, entityId) {
    var request =
        '<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 xhr = new XMLHttpRequest();
    xhr.open('POST', '/MSCRMservices/2007/crmservice.asmx', false);

    xhr.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
    xhr.setRequestHeader('SOAPAction', 'http://schemas.microsoft.com/crm/2007/WebServices/Execute');

    xhr.send(request);
}

但是,我想使用 CRM 2011 服务编写此代码以提高 future 版本的可维护性。到目前为止,这是我尝试过的方法,但这不起作用 - 调用的返回码是 HTTP 500(内部服务器错误)。

function invokeWorkflow(workflowId, entityId) {
    var request =
        '<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">' +
        '  <soap:Body>' +
        '    <Execute xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services">' +
        '      <Request xsi:type="ExecuteWorkflowRequest">' +
        '        <EntityId>' + entityId + '</EntityId>' +
        '        <WorkflowId>' + workflowId + '</WorkflowId>' +
        '      </Request>' +
        '    </Execute>' +
        '  </soap:Body>' +
        '</soap:Envelope>';

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

有人知道第二个脚本有什么问题吗?我已经尽我所能尝试使用谷歌搜索,但我发现每个声称适用于 CRM 2011 的示例实际上只是使用 CRM 4 兼容性服务(如第一个示例)。我以 CRM 2011 SDK 中的示例中的第二个示例为基础,尽管这不包括 ExecuteWorkflowRequest 对象的示例,因此它只是最佳猜测。

谢谢!

最佳答案

在 CRM sdk 文件夹\samplecode\cs\client\soaplogger 中有一个名为 SOAPLogger 的应用程序,它在 javascript 中为特定操作生成请求。

在下面,您可以找到“ExecuteWorkflow”的 http 请求(只需更改 EntityIdValueWorkflowIdValue 的值)。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <Execute xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <request i:type="b:ExecuteWorkflowRequest" xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts" xmlns:b="http://schemas.microsoft.com/crm/2011/Contracts">
        <a:Parameters xmlns:c="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
          <a:KeyValuePairOfstringanyType>
            <c:key>EntityId</c:key>
            <c:value i:type="d:guid" xmlns:d="http://schemas.microsoft.com/2003/10/Serialization/">EntityIdValue</c:value>
          </a:KeyValuePairOfstringanyType>
          <a:KeyValuePairOfstringanyType>
            <c:key>WorkflowId</c:key>
            <c:value i:type="d:guid" xmlns:d="http://schemas.microsoft.com/2003/10/Serialization/">WorkflowIdValue</c:value>
          </a:KeyValuePairOfstringanyType>
        </a:Parameters>
        <a:RequestId i:nil="true" />
        <a:RequestName>ExecuteWorkflow</a:RequestName>
      </request>
    </Execute>
  </s:Body>
</s:Envelope>

XMLHttpRequest 的构造是正确的,因此请尝试更改 soapEnvelope

关于javascript - 在 CRM 2011 中从 JavaScript 执行工作流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11545895/

相关文章:

javascript - 如何将文本转换为 Angular 格式的 json 以及如何循环数组

javascript - Timeago Jquery 插件不工作

scala - Scala 中的成功/失败链模式

dynamics-crm-2011 - 表单中的字段名称在 Dynamics CRM 2016 中被截断

plugins - CRM 无法将文件或程序集加载到我的项目中

javascript - 根据子组件的数据计算父组件的状态 : ReactJS

javascript - Ajax 和 Json 完成事件不会触发 Google map

version-control - Mercurial:表示分支已通过测试?

python - 查看流.io : implementing a queue task