Monday 13 March 2017

WebAPI Retrieve Records



Below is the sample query to get records,

function retrieveRecordsWebAPI(query)
{
try
{
var data;
var serverURL = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest();
req.open("GET", serverURL + "/api/data/v8.0/" + query, false);
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.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
req.onreadystatechange = function ()
{
if (this.readyState == 4 )
{
req.onreadystatechange = null;
if (this.status == 200)
{
data = JSON.parse(this.response);
}
else
{
var error = JSON.parse(this.response).error;
alert(error.message);
}
}
};
req.send();
return data;
}
catch (e)
{
Xrm.Page.ui.setFormNotification("Error occured in function : Common.retrieveRecordsWebAPI()" + ".Details : " + (e.description || e.message), "Error");
}
}

Call above function like below,

function getDataUsingWEBAPI()
{
     var cusId = Xrm.Page.getAttribute("customerid").getValue()[0].id;
var columnSet = "?$select=address1_line1,address1_line2";
var query = "accounts" + "(" + cusId.substring(1, 37) + ")" + columnSet;
var response = retrieveRecordsWebAPI(query);
}

MSCRM Early Bound vs Late Bound

Early Bound:

  1. The code generation tool (CrmSvcUtil) creates early-bound entity classes
  2. Each entity (System and Custom) has class.
  3. All attributes and relationships are included in the generated classes. 
  4. When we write  code intelligence support for entity names ,attributes and relationships
  5. No compilation errors of attributes and entities.
  6. Early bound saves compilation time.
  7. We have to generate wrapper class every time to use the newly created entities or new fields
  8. Over network performance is low because of early bound classes are converted to late bound classes.
  9. Code development is faster
  10. compile-time checking of all types so that no implicit casts occur

Late Bound:
  1. Code development is bit slow compared to early bound
  2. Less serialization occurs as entity data is transmitted over a network, which means higher performance
  3. Developer can make spelling mistakes while developing code, which causes run time errors. 
  4. Late binding checks types only when the object is created or an action is performed on the type