繁体   English   中英

Dynamics 365 Web API-创建多个子记录JSON吗?

[英]Dynamics 365 Web API - Create Multiple Child Records JSON?

我们有一个称为Question的实体,它具有多个响应,即Question和Response实体之间存在1:N的关系。

我们已有一个问题记录。 我们需要实现的是更新问题记录,并同时添加所有与同一问题相关的多个响应记录(即,响应表中的new_QuestionId字段应作为响应创建的一部分进行填充)。

我的逻辑是

  1. 更新问题记录(即一次PATCH操作)。
  2. 具有循环结构,并在循环内创建与问题记录相关的响应记录。

样例代码

因此,在这里,我给出了一个示例,其中我们将创建一个与Question相关的响应记录(记录ID为4B5461DB-7061-E711-8124-E0071B66C0A1 )。

POST [Organization URI]/api/data/v8.2/new_responses HTTP/1.1 
Content-Type: application/json; charset=utf-8 
OData-MaxVersion: 4.0 
OData-Version: 4.0 
Accept: application/json
{   
    "new_questionTitle": "This is the question from my console app",  
    "new_score": 100,   
    "new_nativelanguage": "This is in native language",   
    "new_englishtranslation": "This is in english",   
    "new_questionid@odata.bind": "/new_questions(4B5461DB-7061-E711-8124-E0071B66C0A1)",   
    "new_name": "This is the primary attribute" 
}

我的问题是,如果我希望能够创建多个响应,而这些响应都与4B5461DB-7061-E711-8124-E0071B66C0A1的同一问题ID有关,那么它将是JSON

我们在Dynamics 365上在线。

WebAPI可以执行批处理请求,如此处所述: 使用Web API执行批处理操作

Scaleable Solutions的这篇博客文章包含以下用于批处理创建的示例代码:

function BulkCreate() {
    var body = "";
    var entityCollection = new Array();

    var entity1 = {};
    entity1["name"] = "dummy account 1";
    var body = JSON.stringify(entity1);
    entityCollection.push(body);

    body = "";
    var entity2 = {};
    entity2["name"] = "dummy account 2";
    body = JSON.stringify(entity2);
    entityCollection.push(body);

    var data = [];
    data.push('--batch_123456');
    data.push('Content-Type: multipart/mixed;boundary=changeset_BBB456');
    data.push('');

    for (var i = 0; i < entityCollection.length; i++) {
        data.push('--changeset_BBB456');
        data.push('Content-Type:application/http');
        data.push('Content-Transfer-Encoding:binary');
        var id = i + 1;
        data.push('Content-ID:' + id);
        data.push('');
        data.push('POST ' + parent.Xrm.Page.context.getClientUrl() + '/api/data/v8.1/accounts HTTP/1.1');

        data.push('Content-Type:application/json;type=entry');
        data.push('');
        data.push(entityCollection[i]);
    }

    data.push('--changeset_BBB456--');
    data.push('--batch_123456--');
    var payload = data.join('\r\n');
    $.ajax(
    {
        method: 'POST',
        url: parent.Xrm.Page.context.getClientUrl() + '/api/data/v8.1/$batch',
        headers: {
            'Content-Type': 'multipart/mixed;boundary=batch_123456',
            'Accept': 'application/json',
            'Odata-MaxVersion': '4.0',
            'Odata-Version': '4.0'
        },
        data: payload,
        async: false,
        success: function (data, textStatus, xhr) {
            alert("Record has been successfully Created");
        },
        error: function (xhr, textStatus, errorThrown) {
            alert(textStatus + " " + errorThrown);
        }
    });
}

暂无
暂无

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

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