CRM2011 JavaScript Querying using ODATA Rest EndPoint

To use the CRM2011 ODATA Rest EndPoint querying functions you need a Jquery Library, a JSon library and a SDK library embebed in the page-form from where you want to make JavaScript queries.

I have joined these three libraries in a single one called ODATA Libraries.js. Here i attach the file ODATALibraries.doc (cause wordpress dont let me upload .js files). Download it and change the file extension to .js to be able to use it in your CRM2011. ODATALibraries Javascript file

We have to understand that ODATA querying is ASYNCHCHRONOUS which means that our Javascript will make a call and continue running without waiting for a response. Thats why when we make the call we will have to tell the system which function the system will have to call when finishing the execution of our call. We can aswell expecify a function that the system will call in case it has some problem or error during the Query execution.

With this knowledge and the file included in our form as a JavaScript web resource, you will be able to make calls to the CRM2011 webservices this easy way.

Retrieve Single Record

retrieveRecord(id,"AccountSet", retrieveAccountCompleted, function4Errors);

Retrieve Multiple Records

To retrive multiple we need to provide a ODATA qyery where we can specify the fields we want to retrieve, the condition they must complete and the order in which we want the results.

To create this Odata querys i recomend this tool (Odata Query designer) that will generate the queries for you in less than 1 second.

 retrieveMultiple("EntitySet", "?$filter=contactid/Id eq guid'{FFFFFFFFF}'", GetContactsCompleted, function4Errors);

The 4 parameters of this query are:

  1. EntitySet : Entity you want to query.
  2. OData Query: Fields we want to retrieve, condition and order.
  3. Success Callback Function: The control will be returned to this function after the asynchronous call finish execution.
  4. Error Callback Function: In case of error during the query execution the control will be returned to this function.

To process the result data you should do something like this:

function GetContactsCompleted(data, textStatus, XmlHttpRequest)
{
	var accounts = data;
	if (accounts.length > 0)
	{
		for (var i = 0; i < accounts.length; i++)
		{
			var ac = accounts[i];
			if(ac != null && ac.Name != 'myname')
			{
				var name = ac.Name;
				var surname = ac.Surname;
				//......
			}
		};
	}
}

Creating , Updating and Deleting Records.


//Creates an Account record
function createAccount() {

 //Create an object to represent an Account record and supply a single field "Name"
 var account = new Object();
 account.Name = "Sample Account created via OData jQuery library.";
 account.Surname = "Whatever.....";

  //createRecord exists in odatalibreries.js
 createRecord(account, "AccountSet", createAccountCompleted, function4Errors);
}

//Updates an Account record
function updateAccount(id) {

  //Instantiate an Account object and modify some fields. You'll only need to
 //provide the fields you want changed because updateRecord() uses an HTTP MERGE
 var changes = new Object();
 changes.Name = "Updated Sample";
 changes.Telephone1 = "555-0123";
 changes.AccountNumber = "ABCDEFGHIJ";
 changes.EMailAddress1 = "someone1@example.com";

 //updateRecord exists in odatalibreries.js
 updateRecord(id, changes, "AccountSet", updateAccountCompleted, function4Errors);
}

//Prompts user if he/she wants to delete the Account record. Deletes record is yes.

function deleteAccount(id) {
 if (confirm("Do you want to delete this account record?"))
 {
 //deleteRecord exists in odatalibreries.js
  deleteRecord(id, "AccountSet", deleteAccountCompleted, function4Errors);
 }
 }
}

//------------------------------------------------------------
//Callback functions
function function4Errors(XmlHttpRequest, textStatus, errorThrown)
{
    alert('There was an error executing your command.');
}

//Called upon successful Account creation.
function createAccountCompleted(data, textStatus, XmlHttpRequest) {
 //Get back the Account JSON object
 var account = data;
 showMessage("Account created with id: " + account.AccountId.toString());
}

//Called upon successful Account retrieval.
function retrieveAccountCompleted(data, textStatus, XmlHttpRequest) {
 //Get back the Account JSON object
 var account = data;
 showMessage("Account retrieved: account name = \"" + account.Name +
        "\", id = {" + account.AccountId + "}");
}

//Called upon successful Account update. Starts next step: Delete.
function updateAccountCompleted(data, textStatus, XmlHttpRequest) {
 //Get back the Account JSON object
 var account = data;
 showMessage("Account updated: id = " + account.id);
}

//Called upon successful Account deletion.
function deleteAccountCompleted(data, textStatus, XmlHttpRequest) {
 showMessage("Account deleted.");
}

//Function to display information on the host HTML page.
function showMessage(message) {
var div = $("</pre>
<div>").html(message);
 $("#status").append(div);
}

I hope this helps.If you want to get information about Querying CRM2011 using the CrmServiceToolkit SOAP Endpoint read my post here.

Have a nice day.

Hache

Advertisements

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