如何从表单创建注释?

时间:2019-04-24 11:02:13

标签: dynamics-crm odata microsoft-dynamics dynamics-crm-webapi

var note = Object();
  note["notetext"] = "test";
  note["subject"] = "Закрытие обращения";
  note["incidentid@odata.bind"] = `/incidents(FC8F144E-06E7-E711-80C7-0050569B0E28)`;

$.ajax( {
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    url:  'https://crm.lovit.ru/crmlovitdb/api/data/v8.2/annotations',
    async: true,
    data: JSON.stringify( note ),
    beforeSend: function ( XMLHttpRequest )
    {
        XMLHttpRequest.setRequestHeader( "Accept", "application/json" );
        XMLHttpRequest.setRequestHeader( "OData-MaxVersion", "4.0" );
        XMLHttpRequest.setRequestHeader( "OData-Version", "4.0" );
    },
    success: function ( data, textStatus, XmlHttpRequest )
    {
        let result = data;
        alert( "Record created successfully" );
    },
    error: function ( XmlHttpRequest, textStatus, errorThrown )
    {
        alert("ошибка"+ XmlHttpRequest + textStatus + errorThrown);
    }
} );

我需要将表单中的文本发送到循环中的笔记中。我在做什么错了?

2 个答案:

答案 0 :(得分:1)

问题在于单值导航属性,它应该是objectid_incident@odata.bind而不是incidentid@odata.bind

更改此行

note["incidentid@odata.bind"] =  `/incidents(FC8F144E-06E7-E711-80C7-0050569B0E28)`;

如下所示,那么它将起作用。 Read more

note["objectid_incident@odata.bind"] = "/incidents(FC8F144E-06E7-E711-80C7-0050569B0E28)";

Reference

您可以使用CRM REST Builder构建和测试查询,而不会出现语法问题。

enter image description here

答案 1 :(得分:0)

您可能必须在行下方添加反引号

note["incidentid@odata.bind"] ="/incidents(FC8F144E-06E7-E711-80C7-0050569B0E28)";

以下是我尝试并成功运行的以下代码。

var entity = {};
entity.subject = "Test from WEbapi 3";
entity.filename = "File Name 123";
entity["objectid_incident@odata.bind"] = "/incidents(C86A8897-D94F-E911-A82F-000D3A385A1C)";

var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/annotations", 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 === 204) {
            var uri = this.getResponseHeader("OData-EntityId");
            var regExp = /\(([^)]+)\)/;
            var matches = regExp.exec(uri);
            var newEntityId = matches[1];
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send(JSON.stringify(entity));