Introduction:
Microsoft Dynamics 365 July Update, formally known as V9.0 has come up with lots of new and exciting features. No doubt these features have been applauded by end users and developers.
One of the important enhancement that will be boon for developer is Xrm.WebApi. Earlier to v9.0, to perform CRUD operation in Dynamics CRM we need to create our own library or used other third party libraries or used XmlHttpRequest and ajax request. So Xrm.WebApi is a cool enhancement added in Dynamics 365 v9.0 which will help to make developers life simple.
In this blog, we will discuss about how we can perform CRUD operations and work with all data types using newly added WebApi in Xrm namespace.
Using Xrm.WebApi:
Below is the example where we can used Xrm.WebApi.
1. Create:
To create the record, first we need to create the object of entity and then set the required fields and its value and then call Xrm.WebApi.createRecord function to create record in CRM. The parameters of this function are shown below;
1. Entity logical name
2. Entity object
/// This function is used to create a contact record function createContact() { var contactObj = null; try { // create the contact object contactObj = new Object(); contactObj.firstname = "Mike"; contactObj.lastname = "Morgan"; contactObj.creditonhold = false; //set optionsetvalue contactObj.accountrolecode = 2; //set the lookup value contactObj["parentcustomerid_account@odata.bind"] = "/accounts(A8A19CDD-88DF-E311-B8E5-6C3BE5A8B200)" Xrm.WebApi.createRecord("contact", contactObj).then(function (result) { //get the guid of created record var recordId = result.id; //below code is used to open the created record var windowOptions = { openInNewWindow: true }; //check if XRM.Utility is not null if (Xrm.Utility != null) { //open the entity record Xrm.Utility.openEntityForm("contact", recordId, null, windowOptions); } }, function (error) { Xrm.Utility.alertDialog(error.message); }); } catch (e) { Xrm.Utility.alertDialog(e.message); } }
2. Update:
To update the record, first we need to create the object of the entity and then set the fields and its value that we need to update and then call Xrm.WebApi.updateRecord function to create the record in CRM. The parameters of this function are as shown below;
1. Entity logical name
2. Entity guid
3. Entity object
//this function is used to update contact function updateContact() { var ownerId = null; var contactObj = null; try { // create the contact object contactObj = new Object(); //set the properties contactObj.creditonhold = true; //set optionsetvalue contactObj.accountrolecode = 3; //set the lookup value ownerId = "1809D364-F311-4B0A-98D2-198B721B2061"; contactObj["ownerid@odata.bind"] = "/systemusers(" + ownerId + ")" // pass entity logical name , entity guid, entity object Xrm.WebApi.updateRecord("contact", "49A0E5B9-88DF-E311-B8E5-6C3BE5A8B200", contactObj).then(function (result) { Xrm.Utility.alertDialog("Record updated successfully."); }, function (error) { Xrm.Utility.alertDialog(error.message); }); } catch (e) { Xrm.Utility.alertDialog(e.message); } }
3. Delete:
To delete record from CRM use Xrm.WebApi.deleteRecord.
The parameters of this function are shown below;
1. Entity logical name
2. Entity guid
/// This function is used to retrieve the Delete record function deleteRecord() { try { Xrm.WebApi.deleteRecord("contact", "8DA6E5B9-88DF-E311-B8E5-6C3BE5A8B200") .then(function (result) { Xrm.Utility.alertDialog("Record deleted successfully."); }, function (error) { Xrm.Utility.alertDialog(e.message); }); } catch (e) { Xrm.Utility.alertDialog(e.message); } }
4. Retrieve Record:
In order to retrieve record we can use Xrm.WebApi.retrieveRecord. The parameters of this function are as shown below;
1. Entity logical name
2. Entity guid
3. In last parameter pass following [select cause, filter cause etc.]
Note: If you want to get the value of lookup field you have use _ as prefix and _value as suffix. For example for parentcustomerid use _parentcustomerid_value
/// This function is used to retrieve the contact record function retrieveContact() { try { Xrm.WebApi.retrieveRecord("contact", "465B158C-541C-E511-80D3-3863BB347BA8", "$select=fullname,telephone1,preferredcontactmethodcode,createdon,_parentcustomerid_value,creditlimit") .then(function (data) { retrieveContactSuccess(data); }, function (error) { Xrm.Utility.alertDialog(error.message); }); } catch (e) { Xrm.Utility.alertDialog (e.message); } } ///retrieve success function retrieveContactSuccess(data) { try { //get the values //string var fullname = data["fullname"]; //optionset var typeCode = data["customertypecode"]; //lookup var customerGuid = data["_parentcustomerid_value"]; var customerName = data["_parentcustomerid_value@OData.Community.Display.V1.FormattedValue"]; var customerEntityLogicalName = data["_parentcustomerid_value@Microsoft.Dynamics.CRM.lookuplogicalname"]; //money var creditLimit = data["creditlimit@OData.Community.Display.V1.FormattedValue"]; //date var createdonFormattedValue = data["createdon@OData.Community.Display.V1.FormattedValue"]; // gives date in following format 2017-09-30T21:10:19Z var createdon = data["createdon"]; // gives date in following format 10/1/2017 2:40 AM //optionset var preferredConMethod = data["preferredcontactmethodcode@OData.Community.Display.V1.FormattedValue"]; } catch (e) { Xrm.Utility.alertDialog(e.message); } }
Conclusion:
Using Xrm.WebApi in Dynamics 365 v9.0, it is easy to perform CURD operation without performing AJAX request or creating own library or using third party libraries.
One Pic = 1000 words! Analyze data 90% faster with visualization apps!
Get optimum visualization of Dynamics 365 CRM data with –
Kanban Board – Visualize Dynamics 365 CRM data in Kanban view by categorizing entity records in lanes and rows as per their status, priority, etc.
Map My Relationships – Map My Relationships – Visualize connections and relationships between Dynamics 365 CRM entities or related records in a Mind Map view.
create is not working
It would be very helpful if you could elaborate exactly when and where are you facing the issue.
Thanks!
Not working for me as well. I thought it’s not getting into the Xrm.WebApi function. Also not able to debug the successCallback function.
Can you please suggest how to debug the success call back function.
In case of using this XRM function on HTML web resource please make sure you do below two things.
1) Use Xrm.WebApi.createRecord as parent. Xrm.WebApi.createRecord into your script attached in HTML Web Resource .
2) Please add on the head tag of your html web resource.
Thanks!
Not working for me as well. Can you please suggest how to debug successCallback function.
If you want to debug the successful callback then add debugger or break point in line of code highlighted below.
Xrm.WebApi.createRecord(“contact”, contactObj).then(function (result) {
//get the guid of created record
Degugger;
var recordId = result.id;
Hope this will help.
Thanks!
Can we create a dashbord using xrm web.api methods?
If yes can anyone help with that ?
Hi Manish,
You cannot directly create a Dashboard using Xrm.Webapi. But you can try a workaround approach.
You can create a custom action where you will write a code to create a Dashboard and then call that action using Xrm.WebApi.online.execute() method.
Please refer to the following link for creating a dashboard programmatically in C#
https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/customize-dev/create-dashboard
And also refer to the following link for executing the action using Xrm.WebAPI.online.execute method
https://www.inogic.com/blog/2019/01/execute-action-using-xrm-webapi-online-execute-in-dynamics-365-crm-v9-0/
Hope this helps!
Thanks.