Translate

Sunday, 1 December 2013

retrieve with java script in crm 2013

Replacement of SOAP in CRM for single record retrieve in java script.

Hello CRM Lovers,
    First of all thanks for reading my post. As we all know that after CRM Roll-Up 12, SOAP has stopped working for cross browser support in CRM, so we have to write some other code to solve this problem. I found a way to solve this problem so I am sharing it with the beautiful world.
 
    In this post I am going to describe how to retrieve single record with java script of another entity in CRM 2011, CRM 2013 with JSON and OData.It will work on Roll-Up 12, Roll-Up 13 for CRM 2011 and on CRM 2013, and also in cross browsers. It will really help you. For retrieving data please fallow these steps.

  Retrieve Single Record with Java Script in CRM 2013

  Retrieve Multiple Records with Java Script in CRM 2013

  Step 1:-  First of all you have to add this function in your web resource.

//***********************************************************************************************************************
 //*******Retrive Single recored on the basis of the criteria OData Rest //******************************************************************************************

function retrieveRecord(id, odataSetName, successCallback, errorCallback, _excontext) {
    var context = Xrm.Page.context;
    _executionObj = _excontext;
    var serverUrl = context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";

    //id is required 
    if (!id) {
        alert("record id is required.");
        return;
    }     //odataSetName is required, i.e. "AccountSet"
    if (!odataSetName) {
        alert("odataSetName is required.");
        return;
    }
    //Asynchronous AJAX function to Retrieve a CRM record using OData
    $.ajax({
        type: "GET",
        async: false,
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + id + "')",
        beforeSend: function (XMLHttpRequest) {
            //Specifying this header ensures that the results will be returned as JSON.      
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            if (successCallback) {
                successCallback(data.d, textStatus, XmlHttpRequest);
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {
            if (errorCallback)
                errorCallback(XmlHttpRequest, textStatus, errorThrown);
            else
                errorHandler(XmlHttpRequest, textStatus, errorThrown);
        }
    });
}

function errorHandler(xmlHttpRequest, textStatus, errorThrow) {
    alert("Error : " + textStatus + ": " + xmlHttpRequest.statusText);
}


Step 2 :-  Now add the fallowing functions with your custom code. Suppose you want to retrieve a record of a entity then write your function in this format.

//**********************************************************************************//*****************Your custom function for retrieving data********************
//**********************************************************************************
    function FunctionName() {
        var recordId= "guid of the record";
       if (_condition!= null) {
             retrieveRecord(recordId, "EntitySet", getRecord, null, null);
        }
    }

//****************************************************************//****************************************************************

    function getRecord(data, textStatus, XmlHttpRequest) {
        //debugger;      
      if (data != null) {
            
                var attribute1 = data.attribute1 schema name;
                var attribute2 = data.attribute2 schema name;
          
        }
    }

Here  "data" is object of  retrieved records you can find  your required attribute accordingly.

For more info you can visit.

http://msdn.microsoft.com/en-us/library/gg309461.aspx

http://msdn.microsoft.com/en-us/library/hh169248%28v=nav.71%29.aspx

Or you can download odata query designer from this link from here you will get a managed solution import it in your organization and design your odata query according to your requirement. 

http://crm2011odatatool.codeplex.com/


So by fallowing all upper steps you can retrieve records . It will work in cross browser and in all new Roll-Up 12,Roll-Up 13 . 

Enjoy the great technology Dynamics CRM. Good luck.....!!!!! 
Thanks.
Post a Comment