首页 文章

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

提问于
浏览
0

我需要创建一个JS-Library,它可以使用适用于Dynamics CRM 2016的新WebApi运行工作流程:

我需要从我的代码启动工作流程 . (工作流程应该是“实时”)而不是异步 . 我将在表单上构建我的函数调用到Ribbon . 如果有人可以帮助我,我会更感恩,因为我搜索了所有的互联网,但无法找到如何解决这个问题,除了从上面的链接,我发现这个方法

但我不确定如何使用这种方法?一旦agin必须“实时”,我找到了解决方案,例如:

-https://processjs.codeplex.com/

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

3 回答

  • 2

    既然我们已经采取行动,那么就不再需要从javascript启动工作流了 . 我过去常常使用使用SOAP api的javascript库,但web api操作更容易使用 . 并且以与工作流相同的方式创建操作 . 要创建操作,请转到创建工作流,而不是从下拉选择操作中选择工作流 . 你最终会得到一个这样的表格 .
    enter image description here
    记住要对其运行的唯一名称和实体 . 在这个例子中,我将使用图示的工作流程,该工作流程针对联系人记录运行 . 从javascript我现在可以发布POST

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

    同样,这是一个针对联系人并运行wa_GetContactSyncStatus操作的操作,将值更改为您需要的值 . 另外作为旁注,这是针对2016服务器的任何东西,以后将有一个不同的api版本供您使用 . 请参阅crm实例中的开发人员资源页面,以确定您的web api的URL是什么 .

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

    另一方面注意,如果您的操作调用另一个非同步的工作流程,它很可能会在您的异步后台工作流程之前返回 .

  • 1

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

    要进行同步调用,您需要做的就是通过调整 open 语句使 XmlHttpRequest 同步:

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

    我从不使用库来调用webapi,所以很遗憾我无法建议任何特定于库的代码片段,但我认为任何像样的库都可以让你使XHR请求同步 .

    (强制警告:同步请求不是最理想的,浏览器会抱怨它们,我希望Chrome特别会在未来的某个时刻开始打破同步代码) .

  • 1

    JS中的肥皂请求:

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

    例:

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

相关问题