简体   繁体   English

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

[英]Start workflow with javascript in Dynamics CRM 2016

I need to create a JS-Library which can run workflow using new WebApi for Dynamics CRM 2016: 我需要创建一个JS库,该库可以使用新的WebApi for Dynamics CRM 2016运行工作流:

I need to start workflow from my Code. 我需要从代码中启动工作流程。 (workflow should be “real-time”) and not asynchronously . (工作流应该是“实时”),而不是异步的。 I will build my function-call into Ribbon on form. 我将在窗体上的功能区中构建函数调用。 If anyone can help me I would be more then thankful, since I searched all internet and could not found how to solve this, except from above link where I found this method 如果有人可以帮助我,我将不胜感激,因为我搜索了所有互联网,但找不到找到解决方法,除非从上面的链接中找到了该方法

but I'm not sure how to use this method? 但我不确定如何使用此方法? Once agin it has to be “real-time” I found solutions such as: 重新开始后,它必须是“实时的”,我发现了以下解决方案:

-https: //processjs.codeplex.com/ -https://processjs.codeplex.com/

but this does not work for me since it run workflow asynchronously. 但这对我不起作用,因为它异步运行工作流。 It has to be using Web API from link provided above. 它必须使用上面提供的链接中的Web API。 I think that this Web API works only for Microsoft Dynamics 2016 我认为该Web API仅适用于Microsoft Dynamics 2016

Now that we have actions there really isn't a need to start a workflow from javascript anymore. 现在我们有了动作,实际上不再需要从javascript启动工作流。 I used to do so using a javascript library that used the SOAP api but the web api actions are much easier to use. 我曾经使用使用SOAP api的javascript库来做到这一点,但是使用Web api操作要容易得多。 And an action is created in the same way as a workflow. 并以与工作流程相同的方式创建动作。 To create an action go to create a workflow but instead of choosing workflow from the dropdown select action. 要创建动作,请创建工作流,而不是从下拉菜单中选择工作流。 You will end up with a form like this. 您将最终得到这样的表格。 在此处输入图片说明 Remember the unique name and the entity which you will run it against. 记住唯一的名称和您要对其运行的实体。 In this example I'll be using this workflow pictured which runs against a contact record. 在此示例中,我将使用如图所示的此工作流程,该工作流程针对联系人记录运行。 From javascript I can now issue a POST to 从javascript我现在可以发布POST到

https://<your-crm-server>/api/data/v8.0/contacts(<contact-id>)/Microsoft.Dynamics.CRM.wa_GetContactSyncStatus

Again this is an action targeting contacts and running the wa_GetContactSyncStatus action, change the values to what you need them to be. 同样,这是一个针对联系人并运行wa_GetContactSyncStatus操作的操作,将值更改为所需的值。 Also as a side note this is against a 2016 server anything later will have a different api version for you to use. 另外请注意,这是针对2016服务器的,以后任何东西都将具有不同的api版本供您使用。 Consult the developer resources page in your crm instance to figure out what your url for the web api is. 请查阅您的crm实例中的开发人员资源页面,以了解Web API的网址是什么。

The action will run asynchronously and as long as your javascript request is set to be synchronous as well your request will return when the action is complete. 该动作将异步运行,并且只要将您的javascript请求设置为同步,动作完成后您的请求就会返回。

As another side note if you have your action call another workflow that isn't synchronous it will quite probably return before your asynchronous background workflow does. 另外请注意,如果您要执行操作调用另一个不同步的工作流,则很有可能会在异步后台工作流之前返回。

I do this quite often: make the process an Action , they are designed specifically for this purpose (click a ribbon button and invoke what essentially is a workflow through WebAP; they also become custom messages for plugin registration, which is nice in some scenarios). 我经常这样做:使流程成为Action ,它们是专门为此目的设计的(单击功能区按钮并通过WebAP调用本质上工作流程的东西;它们还成为用于插件注册的自定义消息,在某些情况下很好用) 。

To have synchronous invocations all you need to do is to make the XmlHttpRequest synchronous by tweaking the open statement: 要进行同步调用,您需要做的就是通过调整open语句使XmlHttpRequest同步:

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

I never use libraries to invoke the webapi so unfortunately I can't suggest any library-specific piece of code, but I would assume any decent library allows you to make XHR requests synchronous. 我从不使用库来调用webapi,因此不幸的是,我不能建议任何特定于库的代码,但是我假设任何不错的库都允许您使XHR请求同步。

(Mandatory warning: sync requests are suboptimal and browsers do complain about them, I expect Chrome in particular to start breaking sync code at some point in the future). (强制性警告:同步请求不是最佳的,浏览器会抱怨它们,我希望Chrome特别在将来的某个时候开始破坏同步代码)。

Soap Request in JS : JS中的Soap Request:

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

Example: 例:

RunWorkflow(Xrm.Page.data.entity.getId(),"21E95262-5A36-46CA-B5B5-3F5AA539A9AF","https://org.dynamics.com");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM