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

Deep Insert Xrm.WebApi nested createRecord not creating both records

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

从数组负载创建批量记录时如何处理计时问题?我的所有 org_orgtrigger 自定义实体都已创建,但是我的 org_orgtask 实体中的 NONE 已创建,现在只是用长度为 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

生成的 class 中找到这些
/// <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());
    }
  );
}

建议在second 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);    
        }    
      );    

专业提示:您可以按照说明进行深度插入

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

{
 "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)