Home > CRM, Dynamics 365 > D365 – JScript – Deep Insert – Xrm.WebApi

D365 – JScript – Deep Insert – Xrm.WebApi

Dynamics 365/Version 9.x has brought new objects to the Xrm client object model.

Deep Insert - 4

One of the new additions to Xrm object  is ‘WebApi’ where you can perform CRUD operations.

WebAPI provides wrappers for all CRUD operations with in the framework so that we dont need to rely on 3rd party helper libraries. For example XrmServiceToolKit

In this article I am going to explain the Deep Insert using the Xrm.WebApi.

What is a ‘Deep Insert’:

  • Deep Insert is a create operation where we create a primary record along with new related records.
  • In simpler words, In a single ‘Create’ server call, we can create Account and related Contact and Opportunity along with a Task.
    • Account (i.e., Primary)
  • Deep Insert - 2
    • and Related
      • Contact (and set Account.PrimaryContact)
      • Opportunity (and associate with created Account)

Deep Insert - 3

  • Below is the syntax

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

JScript:

Here is the sample code to create an Account and related records.

function createAccountAndRelated() {
// Define data to create primary (i.e.,Account) and related (i.e.,Contact and Oppoerunity) entity records
var data =
{
“name”: “XYZ Account”,
“creditonhold”: false,
“address1_latitude”: 47.639583,
“description”: “Creating Account along with new Primary Contact and Opportunity”,
“revenue”: 5000000,
“accountcategorycode”: 1,
“primarycontactid”:
{
“firstname”: “Rajeev”,
“lastname”: “Pentyala”
},
“opportunity_customer_accounts”:
[
{
“name”: “New Opportunity”,
“Opportunity_Tasks”:
[
{ “subject”: “Task associated to the new opportunity” }
]
}
]
}

// create account record
Xrm.WebApi.createRecord(“account”, data).then(
function success(result) {
console.log(“Account created with ID: ” + result.id);
},
function (error) {
console.log(error.message);
}
);
}

Deep Insert - I

🙂

Advertisement
Categories: CRM, Dynamics 365 Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: