从CRM V9中的JS调用自定义操作时出现错误请求错误

时间:2017-12-29 16:46:22

标签: dynamics-crm microsoft-dynamics dynamics-crm-online dynamics-crm-365-v9

我在CRM在线V9中创建了一个自定义操作,其中包含2个参数,一个实体引用联系人和一个字符串。

我检查了架构名称是否正确(大写和小写),甚至尝试使用rest builder生成代码,但我不断收到“错误请求”错误。

这是我的代码:

var parameters = {};
var contact = {};
contact.primarykeyid = "49A0E5B9-88DF-E311-B8E5-6C3BE5A8B200";//I added an hard coded value for testing
contact["@odata.type"] = "Microsoft.Dynamics.CRM.contact";
parameters.Contact = contact;
parameters.Text = "Some Text";

var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_CreateSMSrecord", 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));

1 个答案:

答案 0 :(得分:0)

好的,我设法找到了问题 我更改了联系参数的代码,解决了问题, 这是我更新的代码:

var parameters = {};
parameters.Contact = { "contactid": "49A0E5B9-88DF-E311-B8E5-6C3BE5A8B200", "@odata.type": "Microsoft.Dynamics.CRM.contact" }
parameters.Text = "I'm from JS";

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: Xrm.Page.context.getClientUrl() + "/api/data/v9.0/new_CreateSMSrecord",
    data: JSON.stringify(parameters),
    beforeSend: function (XMLHttpRequest) {
        XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
        XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
        XMLHttpRequest.setRequestHeader("Accept", "application/json");
    },
    async: false,
    success: function (data, textStatus, xhr) {
        var results = data;
    },
    error: function (xhr, textStatus, errorThrown) {
        Xrm.Utility.alertDialog(textStatus + " " + errorThrown);
    }
});