gpt4 book ai didi

javascript - 深度插入 Xrm.WebApi 嵌套的 createRecord 不创建两条记录

转载 作者:行者123 更新时间:2023-11-30 20:36:39 25 4
gpt4 key购买 nike

尝试使用 Xrm.WebApi 创建自定义实体记录.

从数组负载创建批量记录时如何处理计时问题?我的所有 org_orgtrigger 自定义实体均已创建,但我的 org_orgtask 实体均未创建,现在仅使用长度为 2 的数组进行测试。

for (var i = 0; i < payload.length; i++) {

var entityName = payload[i].primaryobjecttypecode[0].toUpperCase() + payload[i].primaryobjecttypecode.slice(1);

var trigger =
{
"entityname": entityName,
"messagename": payload[i].name,
"name": payload[i].name + " " + entityName
};

Xrm.WebApi.createRecord("org_orgtrigger", trigger).then(

function success(triggerResult) {

triggerId = triggerResult.id;

var task =
{
"taskidreference": payload[i].TaskId,
"enabled": true,
"pointvalue": payload[i].EligiblePoints,
"name": payload[i].Title,
"triggerid@odata.bind": "/triggers(" + triggerId + ")"
};
Xrm.WebApi.createRecord("org_orgtask", task).then(

function success(taskResult) {
console.log("Created task and assigned trigger to it");
},
function (error) {
console.log(error.message);
}
);
},
function (error) {
console.log(error.message);
}
);
}

有效负载数据如下所示

var payload =    
[
{
"name": "Create",
"primaryobjecttypecode": "account",
"TaskId": 12151,
"Title": "Create an Account - Dynamics event",
"Deliverable": "Dynamics",
"EligiblePoints": 77,
"ChallengeId": 8353,
"ChallengeTitle": "My Cool Challenge"
},
{
"name": "Delete",
"primaryobjecttypecode": "account",
"TaskId": 12152,
"Title": "Delete an Account - Dynamics event",
"Deliverable": "Dynamics",
"EligiblePoints": 77,
"ChallengeId": 8353,
"ChallengeTitle": "My Cool Challenge"
}
];

任务和触发器的重要导航属性如下。您可以在使用 CrmSvcUtil.exe 生成的生成类中找到它们

/// <summary>
/// N:1 org_org_orgtrigger_org_orgtask_orgtriggerid
/// </summary>
[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("org_orgtriggerid")]
[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("org_org_orgtrigger_org_orgtask_orgtriggerid")]
public org_orgtrigger org_org_orgtrigger_org_orgtask_orgtriggerid
{
get
{
return this.GetRelatedEntity<org_orgtrigger>("org_org_orgtrigger_org_orgtask_orgtriggerid", null);
}
set
{
this.OnPropertyChanging("org_org_orgtrigger_org_orgtask_orgtriggerid");
this.SetRelatedEntity<org_orgtrigger>("org_org_orgtrigger_org_orgtask_orgtriggerid", null, value);
this.OnPropertyChanged("org_org_orgtrigger_org_orgtask_orgtriggerid");
}
}


/// <summary>
/// 1:N org_org_orgtrigger_org_orgtask_orgtriggerid
/// </summary>
[Microsoft.Xrm.Sdk.RelationshipSchemaNameAttribute("org_org_orgtrigger_org_orgtask_orgtriggerid")]
public System.Collections.Generic.IEnumerable<org_orgtask> org_org_orgtrigger_org_orgtask_orgtriggerid
{
get
{
return this.GetRelatedEntities<org_orgtask>("org_org_orgtrigger_org_orgtask_orgtriggerid", null);
}
set
{
this.OnPropertyChanging("org_org_orgtrigger_org_orgtask_orgtriggerid");
this.SetRelatedEntities<org_orgtask>("org_org_orgtrigger_org_orgtask_orgtriggerid", null, value);
this.OnPropertyChanged("org_org_orgtrigger_org_orgtask_orgtriggerid");
}
}

这是使用我的导航属性进行深度插入所需的正确语法。

var orgTrigger = { };
var orgTask = { };
var orgEntityName = "";

for (var i = 0; i < orgPayload.length; i++) {

orgEntityName = orgPayload[i].primaryobjecttypecode[0].toUpperCase() + orgPayload[i].primaryobjecttypecode.slice(1);
orgTrigger =
{
"org_name": orgPayload[i].name + " " + orgEntityName,
"org_entityname": orgEntityName,
"org_messagename": orgPayload[i].name,
// navigation property (1:N), found in the generated class using the CrmSvcUtil.exe
"org_org_orgtrigger_org_orgtask_orgtriggerid":
[
{
"org_name": orgPayload[i].Title,
"org_orgtaskidreference": orgPayload[i].TaskId,
"org_enabled": true,
"org_pointvalue": orgPayload[i].EligiblePoints
}
]
};
Xrm.WebApi.createRecord("org_orgtrigger", orgTrigger).then(

function success(org_orgtaskResult) {
console.log("Created task and attached trigger to it, trigger is " + orgTrigger);
},
function (error) {
console.log("ERROR: Xrm.WebApi.createRecord " + error.message.toString());
}
);
}

最佳答案

建议在第二个 web api createRecord(“task”, task) 方法中添加 failurecallback 以查看是否抛出任何异常。

Xrm.WebApi.createRecord("trigger", trigger).then(

function success(triggerResult) {

triggerId = triggerResult.id;

var task =
{
"taskidreference": payload[i].TaskId,
"enabled": true,
"pointvalue": payload[i].EligiblePoints,
"name": payload[i].Title,
"triggerid@odata.bind": "/triggers(" + triggerId + ")"
};

Xrm.WebApi.createRecord("task", task).then(

function success(taskResult) {
//do nothing
},
function (error) {
debugger;
console.log(error.message);
}
);

},
function (error) {
console.log(error.message);
}
);

专业提示:您可以像 here 解释的那样进行深度插入。

更新:
深度插入触发器 + 任务的有效载荷示例如下:(请对此进行测试)

{
"name": "Create account",
"entityname": "account",
"messagename": "Create"

"trigger_tasks_relationship":
[
{
"name": "Create an Account - Dynamics event",
"taskidreference": 12151,
"enabled": true,
"pointvalue": 77
}
]
}

它应该是 org_org_orgtrigger_org_orgtask_orgtriggerid,以验证并确保(区分大小写)执行此操作。下载 Odata 元数据并搜索实体 org_orgtrigger,然后查找 org_orgtask 的集合值导航属性 (1:N)。

enter image description here

关于javascript - 深度插入 Xrm.WebApi 嵌套的 createRecord 不创建两条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49764222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com