Query Data Using Web API

Sometimes you need to retrieve the data of different entity using javaScript and use that data to perform some business logic. For this Purpose you can use either XmlHttp request or Web API request.

In this blog we will see how to retrieve the single record using Web API

1. Retrieve Single Record Using Web API : 

Parameters used in webApi to retrieve single record are : 

1. Entity logical name, 
2. Guid of record, 
3. columns which needs to retrieve from that record.
Xrm.WebApi.online.retrieveRecord("account", "0b4ecbb5-e574-eb11-a812-0022481a9090", "?$select=address1_city,address1_country").then(
    function success(result) {
        var address1_city = result["address1_city"];
        var address1_country = result["address1_country"];
        //perform your business logic here
    },
    function(error) {
        Xrm.Utility.alertDialog(error.message);
    }
);
You can get GUID of record which needs to retrieve using lookup field present on current entity form.

Note: In case if you are not familiar with syntax of WebApi queries, then you can import CRM Rest builder solution into your environment and use that tool to generate queries and later you can modify the same as per your needs.

2. Retrieve Multiple Records Using Web API : 

Parameters used in Web API to retrieve multiple records are : 
1. Entity Logical Name, 
2. Columns which needs to be retrieved, 
3. Query to get records
 
Xrm.WebApi.online.retrieveMultipleRecords("account", "?$select=address1_country,pol_region&$filter=address1_city eq 'Mumbai'").then(
    function success(results) {
        for (var i = 0; i < results.entities.length; i++) {
            var address1_country = results.entities[i]["address1_country"];
            var pol_region = results.entities[i]["pol_region"]; //return value of optionset
            var pol_region_formatted = results.entities[i]["pol_region@OData.Community.Display.V1.FormattedValue"]; //return text of optionset
            //prerform your business logic here!
        }
    },
    function(error) {
        Xrm.Utility.alertDialog(error.message);
    }
);
Above query to retrieve multiple records using WebAPI is basic query.  
If you want to perform more advance queries you can use following link to get information about different filter operator: 

Comments

Popular posts from this blog

Accessing Fields on QuickView Form through javaScript

ADF - (Part 2) Integrate Data From CRM to External system

Power Apps Portals - Lock/Unlock User Account for Invalid Sign In Attempt