Translate

Sunday 6 March 2016

Retrieve record in CRM 2016 using CRM web API with java script

Retrieve record in CRM 2016 using CRM web API with java script.

Hello CRM Developers,
          Here is my new post, In this post I am going to to tell how to retrieve record in dynamics CRM 2016 using CRM web API.

In new CRM 2016 release there is a new way introduced for operation like create, update, delete,retrieve, multiple retrieve etc, web API. CRM Organization service is going to obsolete in this CRM by Microsoft. Web API has been introduced so that communication between CRM and and other platform application can be done easily. New CRM web API is fully support JSON format.

Below is the sample java script code for retrieving record in CRM using java script.


function retrieveRecord() {    
    var clientURL = Xrm.Page.context.getClientUrl();
    var req = new XMLHttpRequest()
    var query = "/api/data/v8.0/accounts?$select=name&$top=1";
    req.open("GET", encodeURI(clientURL + query), true);   
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function () {
        if (this.readyState == 4) {
            req.onreadystatechange = null;
            if (this.status == 200) {
                var data = JSON.parse(this.response);
                if (data && data.value) {
                    for (var indxAccounts = 0; indxAccounts < data.value.length; indxAccounts++) {
                        var accountName = data.value[indxAccounts].name;
                        var eTag = data.value[indxAccounts]['@odata.etag'];
                    }
                }
            }
            else {
                var error = JSON.parse(this.response).error;
                alert("Error retrieving Accounts – " + error.message);
            }
        }
    };
    req.send(null);
}

Only query will change for different type of retrieve. Below are the different retrieve queries.

1. Retrieve top 1 record
   
var query = "/api/data/v8.0/accounts?$select=name&$top=1"
      
2. Retrieve records with lookup condition like records owned bay a user.

var query = "/api/data/v8.0/accounts? $select=name,accountnumber,_primarycontactid_value,createdon,accountcategorycode,revenue&$filter=_ownerid_value eq (212626EE-54B9-4E99-93F9-0BF5FDCA3481)"

3. Retrieve accounts whose name starts with B or C.

var query ="accounts?$filter=startswith(name,'B') or startswith(name,'C')"

4. Retrieve related entity attributes.

var query ="/api/data/v8.0/accounts(B14CC863-07E3-E511-80EE-3863BB2E4DE0)?$select=name,accountnumber,_primarycontactid_value,createdon,accountcategorycode,revenue&$orderby=name&$expand=primarycontactid($select=fullname,telephone1,email)" 

For further help you can leave a comment.

Enjoy the great technology Dynamics CRM. Good Luck .....!!!!
Thanks

Saturday 5 March 2016

Create record in CRM 2016 using CRM web API with java script.

Create record in CRM 2016 using web API with java script.

Hello CRM Developers,
          Here is my new post, In this post I am going to to tell how to create record in dynamics CRM 2016 using CRM web API.

In new CRM 2016 release there is a new way introduced for operation like create, update, delete,retrieve, multiple retrieve etc, web API. CRM Organization service is going to obsolete in this CRM by Microsoft. Web API has been introduced so that communication between CRM and and other platform application can be done easily. New CRM web API is fully support JSON format.

Below is the sample java script code for creating the record in CRM using java script.



function cerateRecord(clientURL)
{  
    //clientURL = your CRM organization URL.  
    var req = new XMLHttpRequest()    
    req.open("POST", encodeURI(clientURL + "/api/data/v8.0/accounts"), true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function () {
        if (this.readyState == 4 /* complete */) {
            req.onreadystatechange = null;
            if (this.status == 204) {
                var accountUri = this.getResponseHeader("OData-EntityId");                
            }
            else {
                var error = JSON.parse(this.response).error;
            }
        }
    };
    var account = new Object();
    //String Field
    account.name = "Test Neo";
    //Option Set field
    account.ownershipcode = 1;
    //Two option
    account.donotemail = true;
    //Currency
    account.creditlimit = 12345;
    //Whole number
    account.numberofemployees = 28;
    //Lookup
    account["primarycontactid@odata.bind"] = "/contacts(C8944807-EFD9-E511-80EC-3863BB2E4DE0)"
    var response = req.send(JSON.stringify(account));
}


For further help you can leave a comment.

Enjoy the great technology Dynamics CRM. Good Luck .....!!!!
Thanks