Deep Insert Xrm.WebApi嵌套的createRecord不创建两个记录

时间:2018-04-10 23:28:12

标签: javascript dynamics-crm dynamics-365 xrm dynamics-crm-webapi

使用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

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

1 个答案:

答案 0 :(得分:1)

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

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,以验证&amp;确保(因为它区分大小写)这样做。 下载Odata元数据&amp;搜索实体org_orgtrigger,然后查找org_orgtask的集合值导航属性(1:N)。

enter image description here