30 Dec’17

Dynamics 365, EnterpriseCreate and Associate records using Xrm.WebApi

Introduction:

In this blog we will demonstrate how to create and associate records using Xrm.WebApi which provides methods to use Web Api to create and manage records.

Implementation:

Step 1: The syntax to create a new entity record is as follows:

Xrm.WebApi.createRecord(entityLogicalName,data).then(sucessCallback,errorCallback);

Here entityLogicalName(string) and data(object) parameters are required. The “data” parameter is a JSON object defining the attributes and values for the new record.

Step 2: In this example we will create a sample account record along with a primary contact for that account, associate an opportunity to the account and create task and notes for the opportunity all in a single operation. This type of process is called as a deep insert.

The code for the same is as shown below:

var scripting = {

recordCreation: function () {

var data = {

"name": "CRAYONS LTD.",

"description": "This Account is Created using a Web API",

"creditonhold": false,

"telephone1": "9954565154",

"address1_city": "Mumbai",

"primarycontactid": {

"firstname": "Clinton",

"lastname": "Dmello"

},

"opportunity_customer_accounts": [

{

"name": "Opportunity Associated to CRAYONS LTD.",

"Opportunity_Tasks": [

{ "subject": "Task Created" }

],

"Opportunity_Annotation": [

{

"subject": "Note Created",

}

]

}

]

}

Xrm.WebApi.createRecord("account", data).then(

function success(result) {

console.log("Account Created with ID" + result.id);

//perform operations on record creation

},

function (error) {

console.log(error.message);

}

);

}

};

Step 3: In order to associate to an existing record @odata.bind annotation can be used.

For Example: “primarycontactid@odata.bind”:”/contacts(GUID of the contact)”.

Screenshots
1. A New Account is created with the name “CRAYONS LTD” and in the primary contact file the primary contact is set with the name Clinton Dmello as set in the code.
New Account Created

2. Also an opportunity is associated to the account as shown below in the associated view.

Opportunity associate to Account created

3. In the opportunity we can see the newly created task in the activities and a note as shown below.

Note Created

Task Created

Hope this helped!

Written by

Clinton D'Mello

Trainee Software Engineer

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.

Want to streamline your business processes?

  • This field is for validation purposes and should be left unchanged.

Recent Articles

  • Commands to Import .bacpac file to D3FOE SQL Server

    14 May’ 2018

    Introduction: This blog article will explain how to import a .bacpac file to Microsoft SQL Server which is created from ...

    Read more
  • [Solved] – Backspace not working in HTML Web Resources

    11 May’ 2018

    Issue: When we use a HTML web resource in Dynamics 365 CRM forms or even as a separate navigation, we sometimes face is...

    Read more
  • Dynamics 365 Tip: Sending Emails using Unified Interface

    10 May’ 2018

    Introduction: The unified client which was released D365 V9 had a very refreshing UI and lots of features. One of the b...

    Read more
  • Filtering Records on BPF unlike on Form – D365

    10 May’ 2018

    Introduction: Working with D365 is best when you’re trying to use as much OOB stuff as possible. Simplest configurati...

    Read more
  • Filter records in gallery control based on value selected in other gallery control in PowerApps.

    10 May’ 2018

    Introduction: This blog explains how to Filter records in gallery control based on value selected in other gallery cont...

    Read more