D365 – JScript – Deep Insert – Xrm.WebApi
Dynamics 365/Version 9.x has brought new objects to the Xrm client object model.
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)
- and Related
- Contact (and set Account.PrimaryContact)
- Opportunity (and associate with created Account)
- and Related
- 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);
}
);
}
🙂