Archive for March 11, 2018

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);


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,
“firstname”: “Rajeev”,
“lastname”: “Pentyala”
“name”: “New Opportunity”,
{ “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: ” +;
function (error) {

Deep Insert - I


Categories: CRM, Dynamics 365 Tags: ,