How to retrieve a record in Dynamics 365 Online V 9.X using JavaScript WebAPI?

Below is the latest Syntax available to retrieve a record in Dynamics 365 Online V9.X using JavaScript,

Syntax:

Xrm.WebApi.retrieveRecord(entityLogicalName, id, options).then(successCallback, errorCallback);

Parameters:

Name Type Required Description
entityLogicalName String Yes The entity logical name of the record you want to retrieve. For example: “account”.
id String Yes GUID of the entity record you want to retrieve.
options String No OData system query options, $select and $expand, to retrieve your data.

·        Use the $select system query option to limit the properties returned by including a comma-separated list of property names. This is an important performance best practice. If properties aren’t specified using $select, all properties will be returned.

·        Use the $expand system query option to control what data from related entities is returned. If you just include the name of the navigation property, you’ll receive all the properties for related records. You can limit the properties returned for related records using the $select system query option in parentheses after the navigation property name. Use this for both single-valued and collection-valued navigation properties.

You specify the query options starting with ?. You can also specify multiple query options by using & to separate the query options. For example:

?$select=name&$expand=primarycontactid($select=contactid,fullname)

See examples later in this topic to see how you can define the options parameter for various retrieve scenarios.

successCallback Function No A function to call when a record is retrieved. A JSON object with the retrieved properties and values will be passed to the function.
errorCallback Function No A function to call when the operation fails.

Return Value:
On success, returns a promise containing a JSON object with the retrieved attributes and their values.

Example:
Create a new Javascript Webresource (new_Account.js) and copy paste the below code in it and call it on OnLoad of the Account Form.

After that, open an Existing Account Record.

function RetrieveAccountRecord() {
 var accountRecordGuid = null, nextLine = "\n";
 accountRecordGuid = Xrm.Page.data.entity.getId();

if (accountRecordGuid != null && accountRecordGuid != "") {
 // Retrieve Record
 Xrm.WebApi.retrieveRecord("account", accountRecordGuid, "?$select=name,creditonhold,description,revenue,industrycode,_primarycontactid_value").then(
 function success(result) {
 // Perform operations on record retrieval
 var sampleValueText = "Retrieved values: ";

// Single Line of Text
 if (result.name != null) {
 sampleValueText += "Account Name : " + result.name + nextLine;
 }

// Two Option Set
 if (result.creditonhold != null) {
 sampleValueText += "CreditOnHold : " + result.creditonhold + nextLine;
 }

// Multiple Lines of Text
 if (result.description != null) {
 sampleValueText += "Description : " + result.description + nextLine;
 }

// Currency Field
 if (result.revenue != null) {
 sampleValueText += "Revenue : " + result.revenue + nextLine;
 sampleValueText += "Revenue (Formatted Text) : " + result['revenue@OData.Community.Display.V1.FormattedValue'] + nextLine;
 }

// Option Set
 if (result.industrycode != null) {
 sampleValueText += "Industry Text : " + result['industrycode@OData.Community.Display.V1.FormattedValue'] + nextLine;
 sampleValueText += "Industry Value : " + result.industrycode + nextLine;
 }

// Lookup
 if (result._primarycontactid_value != null) {
 sampleValueText += "Primary Contact GUID: " + result._primarycontactid_value + nextLine;
 sampleValueText += "Primary Contact Logical Name: " + result['_primarycontactid_value@Microsoft.Dynamics.CRM.lookuplogicalname'] + nextLine;
 sampleValueText += "Primary Contact Text: " + result['_primarycontactid_value@OData.Community.Display.V1.FormattedValue'] + nextLine;
 }

Xrm.Utility.alertDialog(sampleValueText, null);
 },
 function (error) {
 // handle error conditions
 Xrm.Utility.alertDialog("Error while retrieving the Account Record : " + error.message, null);
 }
 );
 }
}

Output:
Dynamics 365 Retrieve Record V9.X

Hope you have successfully retrieved Account record using New Syntax.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.