Hi,
For who are new with Microsoft CRM 2011, this is a first lession that you should know: How to create, update, retrieve and delete a record of an entity in CRM by using javascript. These basic operations help you a lot in developing your CRM by using the client-side language Javascript.
There are two ways to achieve the results:
1. Using directly the REST Endpoint method like in SDK sample in this link: http://msdn.microsoft.com/en-us/library/gg334427.aspx --> for me, this way is complicated, because you have to bring the whole REST Endpoint function to your code, which makes your code like a mess.
2. Download the REST Endpoint method from SDK and use it like a library for your javascript code.
Now are steps how to do the second way:
- Step 1: Prepare the following required js:
+ JavascriptRESTDataOperations --> from SDK: SampleCode\JS\RESTEnpoint\JavascriptRESTDataOperations
+ jquery1.7.1.js --> from http://jquery.com/
+ json2.js --> from http://www.json.org/json2.js
- Step 2: Creating your javascript as following:
Save all of above code in CRMObject.js
- Step 3: Add all 4 above js files in your entity's form. Put the function name for onload or onsave event. Then you could do anything you want now !
I hope it helps. Please email to linhhk87@gmail.com if you have any further question to implement it !
For who are new with Microsoft CRM 2011, this is a first lession that you should know: How to create, update, retrieve and delete a record of an entity in CRM by using javascript. These basic operations help you a lot in developing your CRM by using the client-side language Javascript.
There are two ways to achieve the results:
1. Using directly the REST Endpoint method like in SDK sample in this link: http://msdn.microsoft.com/en-us/library/gg334427.aspx --> for me, this way is complicated, because you have to bring the whole REST Endpoint function to your code, which makes your code like a mess.
2. Download the REST Endpoint method from SDK and use it like a library for your javascript code.
Now are steps how to do the second way:
- Step 1: Prepare the following required js:
+ JavascriptRESTDataOperations --> from SDK: SampleCode\JS\RESTEnpoint\JavascriptRESTDataOperations
+ jquery1.7.1.js --> from http://jquery.com/
+ json2.js --> from http://www.json.org/json2.js
- Step 2: Creating your javascript as following:
//Create record
function Create_CRMObject()
{ var CRMObject = new Object();
CRMObject.Id = "";
//createRecord function is in jsoperation.js
createRecord(CRMObject,"your entity set name", createCRMObjectCompleted,errorCreate)
// entity set name, for example: AccountSet, ContactSet, etc
}
createCRMObjectCompleted = function (data, textStatus, XmlHttpRequest)
{ var newCRMObjectCreated = data["d"];
}
errorCreate = function (XMLHttpRequest, textStatus, errorThrown)
{ alert("Error");
}
//Update record
function Update_CRMObject()
{
new CRMObject = new Object();
var CRMObject_Id = "put the GUID here";
var CRMObject."Attribute" = "put any attribute value here"
//updateRecord function is in jsoperation.js
updateRecord(CRMObject_Id,CRMObject,"you entity set name", updateCRMObjectCompleted,errorUpdate); // entity set name, for example: AccountSet, ContactSet, etc
}
updateCRMObjectCompleted = function (data, textStatus, XmlHttpRequest)
{ alert("your update is successful");
}
errorUpdate = function (XMLHttpRequest, textStatus, errorThrown)
{ alert("Error");
}
//Delete Record
function Delete_CRMObject()
{ var CRMObject_Id =" put the GUID here");
//deleteRecord function is in jsoperation.js
deleteRecord(CRMObject_Id,"your entity set name", deleteCRMObjectCompleted, errorDelete);
}
deleteCRMObjectCompleted = function (data, textStatus, XmlHttpRequest)
{}
errorDelete = function (XMLHttpRequest, textStatus, errorThrown)
{ alert("Error");
}
//Retrieve Record
function retrieve_CRMObject()
{ //retrieveRecord function is in jsoperation.js
retrieveRecord("put your record GUID here","put your entity set name", retrieveCRMObjectCompleted, errorRetrieve);
// entity set name, for example: AccountSet, ContactSet, etc
}
retrieveCRMObjectCompleted = function (data, textStatus, XmlHttpRequest)
{ var newCRMObjectRetrieve = data["d"];
//now you could retrieve any field belongs to your record
}
errorRetrieve = function (data, textStatus, XmlHttpRequest)
{ alert("Error");
}
//Retrieve Multiple Records
function retrievemultiple_CRMObject()
{ retrieveMultiple("your entity set name","", retrievemultipleCRMObjectCompleted, errorRetrieveMultiple);
// entity set name, for example: AccountSet, ContactSet, etc
}
retrievemultipleCRMObjectCompleted = function (data, textStatus, XmlHttpRequest)
{ var CRMObject = data;
if (CRMObject.length !==0)
{ for (i=0, i<CRMObject.length, i++)
{ CRMObject[0].id = ... //retrieve any field you want
}
}
else
{ alert("no record");
}
}
errorRetrieveMultiple = function (data, textStatus, XmlHttpRequest)
{ alert("Error");
}
Save all of above code in CRMObject.js
- Step 3: Add all 4 above js files in your entity's form. Put the function name for onload or onsave event. Then you could do anything you want now !
I hope it helps. Please email to linhhk87@gmail.com if you have any further question to implement it !