REST – Retrieve Example

REST CRUD (Create, Read, Update and Delete) Operations:

Initial Setup:

Download the Latest SDK.

For CRUD Operations, get the following Script files from the path

SDK\SampleCode\JS\RESTEndpoint\JavaScriptRESTDataOperations\JavaScriptRESTDataOperations\Scripts

Initial Setup

Create two JScript Web Resources

SDK Rest Webresource

Create a JScript Web Resource for CRUD Operations,

SDK Operations JScript

Retrieve Example:

Syntax: SDK.REST.retrieveRecord(id, type, select, expand, successCallback, errorCallback) 

Name Type Description
id String A String representing the GUID value for the record to retrieve.
type String The Schema Name of the Entity type record to retrieve.
select String A String representing the $select OData System Query Option to control which attributes will be returned. This is a comma separated list of Attribute names that are valid for retrieve.
If null all properties for the record will be returned
expand String A String representing the $expand OData System Query Option value to control which related records are also returned. This is a comma separated list of up to 6 entity relationship names
If null no expanded related records will be returned.
successCallback Function The function that will be passed through and be called by a successful response.
This function must accept the returned record as a parameter.
errorCallback Function The function that will be passed through and be called by a failed response.
This function must accept an Error object as a parameter.

 Task:Get the Contact details based on Contact Guid using Retrieve Method

Solution: Add the below Script in “new_sdkOperations” Webresource for Retrieving Contact details based on “ContactId”,

function retrieveContact(ContactId) {
SDK.REST.retrieveRecord(ContactId, "Contact", null, null, getDetails, errorHandler);
}

function getDetails(contact) {
alert("Full Name : "+ contact.FullName + "\n" +
"Company Name : " + contact.ParentCustomerId.Name + "\n" +
"Guid : " + contact.ContactId +"\n" +
"Preferred Method of Contact : " + contact.PreferredContactMethodCode.Value +"\n" +
"Bulk Email : " + contact.DoNotBulkEMail +"\n"+
"Birthday : " + contact.BirthDate
);
}

function errorHandler(error) {
alert(error.message);
}

Save and Publish the Web Resource “new_sdkOperations”.

Add the 3 Web Resources in Contact Entity as follows,

Webresources on Contact Form

Add OnLoad event, Library: new_sdkOperations &function retrieveContact

Attach Onload Event

Pass the Existing Contact Guid as a Parameter to test the function,

Pass Contact Parameter to Form

Save and Publish the entity. Open any record to get the below alert.

Final Output

Please provide your valuable comments on this article.

Advertisements

5 thoughts on “REST – Retrieve Example

    • Hi,

      We have an Xrm method to call current record Guid. Please find the below,

      function retrieveContact() {
      var ContactId = Xrm.Page.data.entity.getId();
      SDK.REST.retrieveRecord(ContactId, “Contact”, null, null, getDetails, errorHandler);
      }

      Like

  1. Hi,

    Thanks for the post, It helped me.

    Its perfectly working for my client in CRM2011, but now they’re planning migrate it to CRM2015/2016. Will this code be compatible for it?

    Thanks again.

    Like

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 )

Google+ photo

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

Connecting to %s