1

Tyring to create a custom Entity record using the Xrm.WebApi.

How can I handle timing issues when creating bulk records from an array payload? ALL of my org_orgtrigger custom entities are created, but NONE of my org_orgtask entities are created, right now just testing with an array of length 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,    
           "[email protected]": "/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);    
    }    
  );    
}

The payload data looks like this

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"    
      }    
    ];

The important navigation properties for tasks and triggers is below. You find these in the generated class produced using the 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");
   }
}

This is the correct syntax needed to do deep insert using my navigation properties.

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());
    }
  );
}
3
  • Add failurecallback in Task createRecord & see any exceptions are there.. (you are doing it in Trigger createRecord) Commented Apr 11, 2018 at 1:15
  • odd, I don't hit either of my console logs when I add the callback, have updated the code in the question as well
    – greg
    Commented Apr 11, 2018 at 1:25
  • Check the case sensitive [email protected] schema name; see if triggerResult.id has extra {} braces; you can put a breakpoint inside successcallback & see the execution of promise.. Commented Apr 11, 2018 at 1:44

1 Answer 1

1

It’s advisable to add failurecallback in second web api createRecord(“task”, task) method to see if any exception is thrown.

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,    
              "[email protected]": "/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);    
        }    
      );    

Pro tip: You can do Deep Insert like explained here.

Update:
Sample payload for deep insert of trigger + task will be like: (pls test this)

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

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

It should be org_org_orgtrigger_org_orgtask_orgtriggerid, to verify & make sure (as its case sensitive) do this. Download the Odata Metadata & search for entity org_orgtrigger, then look out for collection valued navigation property (1:N) of org_orgtask.

enter image description here

6
  • can you provide an example of deep insert that uses my custom task entity?
    – greg
    Commented Apr 11, 2018 at 2:06
  • ah I think my error is in the syntax, where is needing the word relationship documented, will try this when back on my dynamics dev machine?
    – greg
    Commented Apr 13, 2018 at 0:05
  • 1
    @GregDegruy you can download Csdl from Dev resources & check the exact navigation property of that relationship.. Commented Apr 13, 2018 at 0:28
  • i think I found the navigation properties using the Csdl, updating my question with them for feedback on which use.
    – greg
    Commented Apr 13, 2018 at 21:56
  • 1
    yes works like a charm, i can deep insert 5 items in bulk. now just testing the scale for this! adding final code to my question, if you can include in your answer as well, thanks
    – greg
    Commented Apr 16, 2018 at 22:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.