我们有一个名为Question的实体,它有多个响应,即问答实体之间存在1:N的关系。
我们有一个现有的问题记录。我们需要能够实现的是更新问题记录,同时添加与同一问题相关的多个响应记录(即响应表上的new_QuestionId字段应作为响应创建的一部分填充)。
我的逻辑是
示例代码
所以,在这里我给出了一个示例,我们正在创建一个与问题相关的响应记录(记录标识为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"
}
如果我希望能够创建多个回复,那么我的问题是什么是JSON,所有回复都与4B5461DB-7061-E711-8124-E0071B66C0A1
我们在线运行Dynamics 365。
答案 0 :(得分:0)
WebAPI具有执行批量请求的功能,如下所示:Execute batch operations using the Web API
可扩展解决方案的this blog post包含以下批量创建的示例代码:
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);
}
});
}