Monday, 27 August 2018

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);



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);
 }
 );
 }
}


No comments:

Post a Comment

Hide New... button on lookup controls in model-driven apps

  The 'New ...' button is shown upon opening the lookup search dialog whenever the logged in user has at least user create privileg...