In this post, I’m going to talk about how to retrieve records in CRM using JavaScript and Xrm.WebApi.retrieveRecord() web API. This is a useful method that allows you to get a single record by its ID and entity name, and optionally specify which attributes and relationships you want to include in the response. I’ll show you some use cases and code examples for each case. Let’s get started!
What is Xrm.WebApi.retrieveRecord() ?
The Xrm.WebApi.retrieveRecord() method is part of the Xrm.WebApi object that provides access to the Dynamics 365 web API. The web API is a RESTful service that supports CRUD (create, read, update, delete) operations on the data stored in Dynamics 365. The web API is based on the OData protocol, which is a standard for querying and manipulating data over the web.
The syntax of the Xrm.WebApi.retrieveRecord() method is as follows:
Xrm.WebApi.retrieveRecord(entityLogicalName, id, options).then(successCallback, errorCallback);
The parameters are:
entityLogicalName: A string that represents the logical name of the entity that you want to retrieve. For example, “account” or “contact”.
id: A string that represents the GUID of the record that you want to retrieve. For example, “a3f6fcb1-5c6f-4b7d-bf6d-4a31b0db0c51”.
options: An optional string that specifies which attributes and relationships you want to include in the response. You can use the OData system query options ($select, $expand, etc.) to construct this string. For example, “$select=name,revenue&$expand=primarycontactid($select=fullname,emailaddress1)”.
successCallback: A function that is executed when the request is successful. The function receives a parameter that contains the retrieved record as an object.
errorCallback: A function that is executed when the request fails. The function receives a parameter that contains an error object.
Use Cases and Code Examples
Now that we know the syntax of the Xrm.WebApi.retrieveRecord() method, let’s see some use cases and code examples for each case.
Retrieve a record with all its attributes with Xrm.WebApi.retrieveRecord()
The simplest use case is to retrieve a record with all its attributes without specifying any options. For example, if we want to retrieve an account record with the ID “a3f6fcb1-5c6f-4b7d-bf6d-4a31b0db0c51”, we can use the following code:
Xrm.WebApi.retrieveRecord("account", "a3f6fcb1-5c6f-4b7d-bf6d-4a31b0db0c51").then(
function (result) {
// Do something with the result
console.log(result.name); // The name of the account
console.log(result.revenue); // The revenue of the account
console.log(result.primarycontactid); // The primary contact of the account as an object
},
function (error) {
// Handle error
console.error(error.message); // The error message
}
);
The result object will contain all the attributes of the account entity as properties. If the attribute is a lookup or a customer field, the result object will also contain an additional property with the suffix “_formatted” that contains the formatted value of the attribute. For example, result.primarycontactid_formatted will contain the name of the primary contact.
Retrieve a record with specific attributes with Xrm.WebApi.retrieveRecord()
If we only want to retrieve some specific attributes of a record, we can use the $select option to specify which attributes we want to include in the response. For example, if we only want to retrieve the name and revenue of an account record, we can use the following code:
Xrm.WebApi.retrieveRecord("account", "a3f6fcb1-5c6f-4b7d-bf6d-4a31b0db0c51", "$select=name,revenue").then(
function (result) {
// Do something with the result
console.log(result.name); // The name of the account
console.log(result.revenue); // The revenue of the account
},
function (error) {
// Handle error
console.error(error.message); // The error message
}
);
The result object will only contain the specified attributes as properties.
Retrieve a record with related records with Xrm.WebApi.retrieveRecord()
If we want to retrieve a record along with its related records, we can use the $expand option to specify which relationships we want to include in the response. For example, if we want to retrieve an account record and its primary contact record, we can use the following code:
Xrm.WebApi.retrieveRecord("account", "a3f6fcb1-5c6f-4b7d-bf6d-4a31b0db0c51", "$expand=primarycontactid").then(
function (result) {
// Do something with the result
console.log(result.name); // The name of the account
console.log(result.primarycontactid); // The primary contact of the account as an object
console.log(result.primarycontactid.fullname); // The full name of the primary contact
console.log(result.primarycontactid.emailaddress1); // The email address of the primary contact
},
function (error) {
// Handle error
console.error(error.message); // The error message
}
);
The result object will contain the primary contact of the account as a nested object under the property primarycontactid. The nested object will contain all the attributes of the contact entity as properties.
We can also use the $select option within the $expand option to specify which attributes of the related records we want to include in the response. For example, if we only want to retrieve the full name and email address of the primary contact, we can use the following code:
Xrm.WebApi.retrieveRecord("account", "a3f6fcb1-5c6f-4b7d-bf6d-4a31b0db0c51", "$expand=primarycontactid($select=fullname,emailaddress1)").then(
function (result) {
// Do something with the result
console.log(result.name); // The name of the account
console.log(result.primarycontactid); // The primary contact of the account as an object
console.log(result.primarycontactid.fullname); // The full name of the primary contact
console.log(result.primarycontactid.emailaddress1); // The email address of the primary contact
},
function (error) {
// Handle error
console.error(error.message); // The error message
}
);
The result object will contain the primary contact of the account as a nested object under the property primarycontactid. The nested object will only contain the specified attributes as properties.
Conclusion
In this post, we learned how to use the Xrm.WebApi.retrieveRecord() web API method to retrieve records in CRM. We saw some use cases and code examples for each case. We learned how to use the options parameter to specify which attributes and relationships we want to include in the response. We also learned how to handle success and error callbacks.
I hope you found this post useful and interesting. If you have any questions or feedback, please leave a comment below. Thanks for reading!
For more in-depth insights and tutorials on Programming or Dynamics 365 features and customization, check out our posts to further enhance your proficiency and maximize the potential of your skills.
Leave a comment