Stephanie Schenk
Mar 26, 2024
Expert articles | 3 min read

In a Dynamics 365 Sales App, I added a custom button in the ribbon bar. The purpose of this button was, originating from an existing task record, to create a new follow-up task record. This new follow-up task record must be linked to the original task record via a custom lookup field on the task table, which I named “Originating Task Id.”

For the creation of the new record via the ribbon button, I use Java Script library Xrm.WebApi.

Xrm.WebApi in Microsoft Dynamics 365 is a powerful JavaScript library that facilitates seamless communication between client-side scripts and the Dynamics 365 Web API, enabling developers to perform CRUD (Create, Read, Update, Delete) operations and interact with data entities within the Dynamics 365 environment.

I faced the challenge of getting the correct property name of the relation between the original task and the follow-up task, which is needed to fill the “Originating Task Id” field when creating the new task record via JavaScript.

The official Microsoft documentation of the library says: “The names of single-valued navigation properties are not always the same as the LogicalName for the lookup attribute. You should make sure you are using the Name attribute value of the NavigationProperty element in the Web API $metadata service document. More information: Web API Navigation Properties.”

But how to find this property of the custom lookup field “Originating Task ID?”

I had to browse the metadata for the OneToMany relationship of my custom lookup with the name originatingtaskid.

I used the helpful tool “Metadata Browser” from XrmToolbox to easily read the metadata.

The property to use in XrmWeb.Api Create function could be found at “ReferencingEntityNavigationPropertyName;” for my custom lookup this was new_originatingtaskid_Task. In the create function, the property has to be combined with the @odata.bind annotation.

ReferencingEntityNavigationPropertyName

Now I was able to create a new task record with reference to the originating task with  Xrm.WebApi.createRecord function:

var activityData = 
{
   "subject": "New Task Subject",
   "new_originatingtaskid_Task@odata.bind": "/tasks(465b158c-541c-e511-80d3-3863bb347ba8)"
};

Xrm.WebApi.createRecord("task", activityData.then(
   function success(result) {
     console.log("Task created with ID: ", + result.id);
   },
   function (error) {
      console.log(error.message);
   }

Do you have any further questions? Please contact us!


Helpful links:

Microsoft Xrm.Web.Api reference
https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/reference/xrm-webapi

XrmToolbox Metadata Browser:
https://www.xrmtoolbox.com/plugins/MsCrmTools.MetadataBrowser/