在Dynamics CRM 2016中使用javascript启动工作流

在Dynamics CRM 2016中使用javascript启动工作流,javascript,dynamics-crm,microsoft-dynamics,dynamics-crm-2016,Javascript,Dynamics Crm,Microsoft Dynamics,Dynamics Crm 2016,我需要创建一个JS库,它可以使用新的WebApi for Dynamics CRM 2016运行工作流: 我需要从我的代码启动工作流。(工作流应该是“实时的”),而不是异步的。我将把函数调用构建到窗体上的Ribbon中。 若有人能帮助我,我会非常感激,因为我搜索了所有的互联网,并没有找到如何解决这个问题,除了从上面的链接,我发现了这个方法 但我不知道如何使用这种方法?一旦进入,它必须是“实时的” 我找到了如下解决方案: -https://processjs.codeplex.com/

我需要创建一个JS库,它可以使用新的WebApi for Dynamics CRM 2016运行工作流:

我需要从我的代码启动工作流。(工作流应该是“实时的”),而不是异步的。我将把函数调用构建到窗体上的Ribbon中。 若有人能帮助我,我会非常感激,因为我搜索了所有的互联网,并没有找到如何解决这个问题,除了从上面的链接,我发现了这个方法

但我不知道如何使用这种方法?一旦进入,它必须是“实时的” 我找到了如下解决方案:

-https://processjs.codeplex.com/


但这不适用于我,因为它异步运行工作流。它必须使用上面提供的链接中的Web API。我认为此Web API仅适用于Microsoft Dynamics 2016

我经常这样做:使流程成为一个
操作
,它们是专门为此而设计的(单击一个功能区按钮并通过WebAP调用基本上是一个工作流;它们也成为插件注册的自定义消息,这在某些情况下很好)

要进行同步调用,只需通过调整
open
语句使
XmlHttpRequest
同步即可:

// 'xhr' is the XMLHttpRequest 
xhr.open(http_method, request_url, false); <-- third parameter 'false' means sync request
/'xhr'是XMLHttpRequest

xhr.open(http_方法,request_url,false);我经常这样做:使流程成为一个
操作,它们是专门为这个目的设计的(单击一个功能区按钮,通过WebAP调用基本上是一个工作流;它们也成为插件注册的自定义消息,这在某些情况下很好)

要进行同步调用,只需通过调整
open
语句使
XmlHttpRequest
同步即可:

// 'xhr' is the XMLHttpRequest 
xhr.open(http_method, request_url, false); <-- third parameter 'false' means sync request
/'xhr'是XMLHttpRequest

open(http\u方法,请求url,false)现在我们有了动作,真的不需要再从javascript启动工作流了。我以前使用一个使用SOAP api的javascript库来启动工作流,但是web api动作更容易使用。动作的创建方式与工作流相同。要创建动作,请创建工作流,而不是选择工作流从下拉菜单中选择操作。您将得到这样一个表单。 请记住唯一的名称和您将对其运行的实体。在本例中,我将使用此工作流,它将针对联系人记录运行。 现在,我可以从javascript向

https:///api/data/v8.0/contacts()/Microsoft.Dynamics.CRM.wa_GetContactSyncStatus

同样,这是一个针对联系人的操作,运行wa_GetContactSyncStatus操作,将值更改为您需要的值。另外,作为补充说明,这是针对2016服务器的。以后的任何操作都会有不同的api版本供您使用。请参阅crm实例中的开发人员资源页面,以了解您的urlweb api是

该操作将异步运行,只要您的javascript请求设置为同步,并且在操作完成时,您的请求将返回


另一方面,如果你让你的动作调用另一个不同步的工作流,它很可能会在你的异步后台工作流返回之前返回。

现在我们有了动作,真的不需要再从javascript启动工作流了。我曾经使用一个使用SOAP api但e web api操作更易于使用。操作的创建方式与工作流的创建方式相同。要创建操作,请转到创建工作流,但不要从下拉菜单中选择“操作”,您将得到如下表单。 请记住唯一的名称和您将对其运行的实体。在本例中,我将使用此工作流,它将针对联系人记录运行。 现在,我可以从javascript向

https:///api/data/v8.0/contacts()/Microsoft.Dynamics.CRM.wa_GetContactSyncStatus

同样,这是一个针对联系人的操作,运行wa_GetContactSyncStatus操作,将值更改为您需要的值。另外,作为补充说明,这是针对2016服务器的。以后的任何操作都会有不同的api版本供您使用。请参阅crm实例中的开发人员资源页面,以了解您的urlweb api是

该操作将异步运行,只要您的javascript请求设置为同步,并且在操作完成时,您的请求将返回

另一方面,如果您让您的操作调用另一个不同步的工作流,它很可能会在您的异步后台工作流返回之前返回。

JS中的Soap请求:

function RunWorkflow(in_entitiId,in_workflowId,in_url) { 
    var _return = window.confirm('Do you want to execute workflow ?');
    if (_return) {
        var url = in_url;
        var entityId =in_entitiId ;
        var workflowId = in_workflowId;
        var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
        url = url  + OrgServicePath;
        var request;
        request = "<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/\">" + entityId + "</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/\">" + workflowId + "</c:value>" +
                            "</a:KeyValuePairOfstringanyType>" +
                            "</a:Parameters>" +
                            "<a:RequestId i:nil=\"true\" />" +
                            "<a:RequestName>ExecuteWorkflow</a:RequestName>" +
                        "</request>" +
                        "</Execute>" +
                    "</s:Body>" +
                    "</s:Envelope>";

        var req = new XMLHttpRequest();
        req.open("POST", url, true)
        // Responses will return XML. It isn't possible to return JSON.
        req.setRequestHeader("Accept", "application/xml, text/xml, */*");
        req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
        req.onerror = displayError;
        req.onreadystatechange = function () { assignResponse(req); };
        req.send(request);
    }
    function displayError(e) {
    alert(this.status);
    }

}

function assignResponse(req) {
    if (req.readyState == 4) {
        if (req.status == 200) {
            alert('successfully executed the workflow');
        }
    }
}
JS中的Soap请求:

function RunWorkflow(in_entitiId,in_workflowId,in_url) { 
    var _return = window.confirm('Do you want to execute workflow ?');
    if (_return) {
        var url = in_url;
        var entityId =in_entitiId ;
        var workflowId = in_workflowId;
        var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
        url = url  + OrgServicePath;
        var request;
        request = "<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/\">" + entityId + "</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/\">" + workflowId + "</c:value>" +
                            "</a:KeyValuePairOfstringanyType>" +
                            "</a:Parameters>" +
                            "<a:RequestId i:nil=\"true\" />" +
                            "<a:RequestName>ExecuteWorkflow</a:RequestName>" +
                        "</request>" +
                        "</Execute>" +
                    "</s:Body>" +
                    "</s:Envelope>";

        var req = new XMLHttpRequest();
        req.open("POST", url, true)
        // Responses will return XML. It isn't possible to return JSON.
        req.setRequestHeader("Accept", "application/xml, text/xml, */*");
        req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
        req.onerror = displayError;
        req.onreadystatechange = function () { assignResponse(req); };
        req.send(request);
    }
    function displayError(e) {
    alert(this.status);
    }

}

function assignResponse(req) {
    if (req.readyState == 4) {
        if (req.status == 200) {
            alert('successfully executed the workflow');
        }
    }
}

你能编辑这个问题并添加一些额外的上下文吗?我很难理解你想通过使用webapi端点执行同步工作流来完成什么。@Federico Jousset我意识到工作流有自己的“实时”设置我使用了processjscodeplex.com/的解决方案,尽管它说它的异步调用,我假设他们考虑了ajax调用,我会在几天内知道这种访问是可以的,但由于我在internet或WebApi文档中没有找到任何其他内容,我猜是可以的。目前我正在尝试阅读workf基于其名称:低:-但我遇到了一些问题。如果有一些进展/问题Creat Dan,我问这个问题主要是因为ExecuteWorkflowRequest当前在WebAPI端点中不可用。如果需要其他内容,请大声喊一声。你能编辑这个问题并添加一些额外的上下文吗?我很难理解是什么是否要使用webapi端点完成同步工作流的执行。@Federico Jousset我意识到工作流有自己的设置,是“实时”的,而不是o