简体   繁体   English

在 Dynamics CRM 中使用 JavaScript 调用全局自定义操作

[英]Calling global custom action using JavaScript in Dynamics CRM

I'm having trouble calling my global custom action using JavaScript in Dynamics CRM.我在 Dynamics CRM 中使用 JavaScript 调用全局自定义操作时遇到问题。

In CRM I created this global action that takes an in parameter and returns an out parameter .在 CRM 中,我创建了这个全局操作,它接受一个输入参数并返回一个输出参数 I've confirmed that the action is activated and works.我已经确认该操作已激活并有效。 The problem arises when I try to call it using JavaScript.当我尝试使用 JavaScript 调用它时出现问题。

My JavaScript code is as follows:我的 JavaScript 代码如下:

callCustomAction: function (actionName, actionParameters) {
    var result = null;

    var req = new XMLHttpRequest();
    req.open("POST", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + actionName), false);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function () {
        if (this.readyState == 4) {
            req.onreadystatechange = null;

            if (this.status == 200) {
                result = JSON.parse(this.response);
            } else {
                var error = JSON.parse(this.response).error;
                alert(error.message);
            }
        }
    };
    req.send(window.JSON.stringify(actionParameters));

    return result;
}

Now, to me this looks correct but I got an issue with the URL which at the moment leads to nowhere.现在,对我来说,这看起来是正确的,但我遇到了 URL 问题,目前无处可去。 Everywhere I look the URL for a global action is simply the organization URL followed by "/api/data/v8.2/" and the name of my action but for me it's not working and I can't figure out why.我到处寻找全局操作的 URL 只是组织 URL,后跟“/api/data/v8.2/”和我的操作名称,但对我来说它不起作用,我不知道为什么。

Resolved The issue was using a mix of EntityReferences and strings as out parameters which made my action not show up under metadata已解决问题是使用 EntityReferences 和字符串的混合作为输出参数,这使我的操作没有显示在元数据下

Global actions which have an output parameter associated cant be called using the Web API.无法使用 Web API 调用具有关联输出参数的全局操作。 This is not the case for actions having no output parameter.对于没有输出参数的操作,情况并非如此。

This is because sometimes the status code is 200 and sometimes different.这是因为有时状态代码是 200,有时不同。 We noticed this bug and observed it to happen only with actions having output parameter.我们注意到了这个错误,并观察到它只发生在具有输出参数的操作中。 Also we noticed this happening on certain times and not frequently as in at times you will get output but sometimes not.我们也注意到这种情况在某些时候发生,并不经常发生,因为有时您会得到输出但有时不会。

As a workaround, you need to use the conventional SOAP query and execute the workflow and then parse the response.作为一种解决方法,您需要使用传统的 SOAP 查询并执行工作流,然后解析响应。

Hope MS solves this soon希望MS尽快解决这个问题

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

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