如何从Javascript Microsoft Dynamics调用Actions

问题描述 投票:1回答:2

我需要从Javascript调用Action Process。我的动作接受2个输入参数和1个输出参数。以下是我的行动的截图

enter image description here我在我的表单中有一个textField,并且在它的onChange事件中我正在调用这个CallAction方法。以下是JavaScript

function CallAction() {

        var actionName = "taqi_getPrice";
        var actionParameters = {
            "base": "USD",
            "TotalPrice": "200"
        };
        var actionResponse = activateCustomAction(actionName, actionParameters);
    }
    function activateCustomAction(actionName, actionParams) {

        var req = new XMLHttpRequest();
        req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/taqi_getPrice", false);
        req.setRequestHeader("OData-MaxVersion", "4.0");

        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 200) {
                    var results = JSON.parse(this.response);
                    alert('Success');
                } else {
                    alert('fail');
                    //Xrm.Utility.alertDialog(this.statusText);
                    console.log(this);
                }
            }
        };
        req.send(JSON.stringify(actionParams));
    }

运行此脚本时,我在chrome控制台中收到以下错误

POST https://techgulf.crm4.dynamics.com/api/data/v9.0/taqi_getPrice 404

有时它也说

Access-Control-Allow-Headers不允许请求标头字段Access-Control-Allow-Headers

javascript dynamics-crm action microsoft-dynamics dynamics-crm-webapi
2个回答
1
投票

好吧,我创建了与你在截图中提到的完全相同的动作,除了我使用的实体是帐户。我使用下面的代码来触发Action,它确实对我没有任何问题,并按预期返回值。

enter image description here

可能是测试你可以提供静态Guid并看看你如何得到结果。

var parameters = {};
parameters.base = "123";
parameters.TotalPrice = "222";
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/accounts(DC86C293-CA4F-E911-A82F-000D3A385A1C)/Microsoft.Dynamics.CRM.crmp_TestAction2", false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send(JSON.stringify(parameters));

0
投票

更改以下行

req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/taqi_getPrice", false);

像下面这样一个:

req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/taqi_cars(" + Id + ")/Microsoft.Dynamics.CRM.taqi_getPrice", false);

基本上我们需要传递实体集的名称,其中包含记录的id,后跟附加了Microsoft.Dynamics.CRM的操作的名称。在全球行动的情况下,我们只需要Microsoft.Dynamics.CRM.<<ActionName>>

Reference

看起来你需要一个同步的Action调用执行(因为你在req.open中使用false),否则你可以使用总是异步的Xrm.WebApi.executeRead more

© www.soinside.com 2019 - 2024. All rights reserved.