Using Xrm.WebApi to execute Web API methods in Dynamics 365
With the release of Dynamics 365 v 9.0 we now have a new library to implement WebAPI methods using Xrm.WebApi. This new feature simplifies a lot the development of JavaScript web resources. Now Instead of writing the complete HTTP request, we can just use the methods from the Web API.
Xrm.WebApi has two properties to use for the Online and the Offline client respectively. In this article, we are going to discuss different crud operations in Xrm.WebApi for the online client.
Create Entity Record
To create an entity record, we can simply call Xrm.WebApi Create method using the following parameters.
// define the data to create new account var data = { "name": "Sample Account", "description": "This is the description of the sample account", } // create account record parent.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); // handle error conditions } );
Create Related Entity Records
We can also create entities related to each other by defining them as navigation properties values. This is known as deep insert. In this example, we will create a sample account record along with the primary contact record and an associated opportunity record.
// define data to create primary and related entity records var data = { "name": "Sample Account", "primarycontactid": { "firstname": "John", "lastname": "Smith" }, "opportunity_customer_accounts": [ { "name": "Opportunity associated to Sample Account", "Opportunity_Tasks": [ { "subject": "Task associated to opportunity" } ] } ] } // create account record parent.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); // handle error conditions } );
Associate Existing Entity Record on Create
To associate new entity records to existing entity records, set the value of single-valued navigation properties using the @odata.bind
annotation. The following example creates an account record, and associates it to an existing contact record to set the latter as the primary contact for the new account record:
var data = { "name": "Sample Account", "primarycontactid@odata.bind": "/contacts(465b158c-541c-e511-80d3-3863bb347ba8)" } // create account record parent.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); // handle error conditions } );
Delete Entity Record
For deleting a record we need to pass the guid for the record we need to delete
parent.Xrm.WebApi.deleteRecord("account", guid-to-delete).then( function success(result) { console.log("Account deleted"); // perform operations on record deletion }, function (error) { console.log(error.message); // handle error conditions } );
Retrieve Single Entity Record
To retrieve a single record by specifying the GUID and entity name we follow the following syntax
parent.Xrm.WebApi.retrieveRecord("account", GUID, "?$select=name,revenue").then( function success(result) { console.dir(result); // perform operations on record retrieval }, function (error) { console.log(error.message); // handle error conditions } );
Retrieve Related Entity Record
The following example demonstrates how to retrieve the contact for an account record with record GUID.
parent.Xrm.WebApi.retrieveRecord("account", GUID, "?$select=name&$expand=primarycontactid($select=contactid,fullname)").then( function success(result) { console.log(`Retrieved values: Name: ${result.name}, Primary Contact ID: ${result.primarycontactid.contactid}, Primary Contact Name: ${result.primarycontactid.fullname}`); // perform operations on record retrieval }, function (error) { console.log(error.message); // handle error conditions } );
Retrieve Multiple Records
To retrieve a collection of entity records we need to follow the following syntax
parent.Xrm.WebApi.retrieveMultipleRecords("account", "?$select=name&$top=3").then( function success(result) { for (var i = 0; i < result.entities.length; i++) { console.log(result.entities[i]); } // perform additional operations on retrieved records }, function (error) { console.log(error.message); // handle error conditions } );
Update a Record
To update an existing record with the GUID we need to use the following syntax
// define the data to update a record var data = { "name": "Updated Sample Account ", "description": "This is the updated description of the sample account", } // update the record Xrm.WebApi.updateRecord("account", GUID, data).then( function success(result) { console.log("Account updated"); // perform operations on record update }, function (error) { console.log(error.message); // handle error conditions } );
The post Using Xrm.WebApi to execute Web API methods in Dynamics 365 appeared first on Scaleable Solutions.
*This post is locked for comments