0

我在我的 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));
4

1 回答 1

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);
    }
});
于 2017-12-30T08:49:25.093 回答