Thursday, 9 February 2012

Create,Update,Retrieve, RetrieveMultiple Records in CRM 2011 with Javascript REST endpoint

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:

//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 !




    

       

13 comments:

  1. how about creating multiple objects?

    ReplyDelete
  2. Hi,

    I have never tried "Create Multiple" objects with Javascript :|. But I did it in plugin and worked well.

    Basically, in CRM SDK there are exactly the same functions in C# (create, retrieve, delete, update...) that you can use for plugin. All need to do is just put the "CreateRecord" function in a loop to execute in multiple times to create multiple objects :).

    I will post a blog about how to use those functions in plugin soon. Thank you for your comment ;)

    ReplyDelete
  3. When I try to update a "built-in" field in a "built-in" entity ("Message" Entity), it works just fine. but when I try to update a custom field (with prefix new_) it doesn't..
    Any ideas..?

    Thanks.

    ReplyDelete
  4. Hi,

    These function work well with any kind of entity or field (system/custom) as long as you put the correct name of the fields or entities.

    For example: you are going to update a new entity named "new_sms", with field named "new_smscontent":

    function Update_CRMObject()
    {
    new CRMObject = new Object();
    var CRMObject_Id = "G18973-29823-321398-12387"; //put the ID of the record you wanna update
    var CRMObject."new_smscontent" = "Hello World"
    updateRecord(CRMObject_Id,CRMObject,"new_smsSet", updateCRMObjectCompleted,errorUpdate);
    }

    Remember to put :
    - Correct entity name: which is schema name
    - Correct field name: which is schema name also
    - Correct Entity Set Name: which is normally = entity schema name + "Set"

    Hope you go through it ;)

    ReplyDelete
  5. Kelly,

    Awesome post. Thanks for this.
    FYI - some of the Syntax in your example is incorrect. I also found that I didn't have to include "Set" after my entity name, it was appending it for me.

    Cheers and thanks again.

    Sean.

    ReplyDelete
  6. Hi,

    useful post - thanks!
    Can you tell me how to retrieve a hierarchy of Subjects, so I can display a string like "grandparent_subject : parent_subject : subject", for example:

    Transport : Cars : Hatchbacks or
    Transport : Cars : SUV or
    Transport : Bikes : 500-750cc or
    Transport : Trucks : Articulated

    ReplyDelete
  7. hi,

    i want to retrieve related values on change of the lookup value. how to use the retrieve function. sample code?

    thanks
    suray

    ReplyDelete
  8. I agree with all of the points keep up the good work.

    warehouse Streamlining & Air freight services

    ReplyDelete
  9. Hi Kelly Hoang

    When I used your code. It is giving me this error createRecord is undefined.

    Thank You

    ReplyDelete
  10. A Python Django course online allows flexible backend learning.It focuses on practical coding tasks.This Python Django course online improves learning flow.It is effective.

    ReplyDelete
  11. Python programming online course offers flexible learning with live projects. It explains core programming concepts clearly. This python programming online course strengthens coding efficiency. Learners practice real-time exercises. Data handling is included. Database integration is covered. Error handling is explained. It builds strong career potential.

    ReplyDelete
  12. Master user-centered design with our training ui ux
    course. Gain hands-on experience in prototyping, usability testing, and visual design for job-ready skills.

    ReplyDelete
  13. Excellent explanation of logical and physical database modeling course
    very helpful for real-time project implementation.

    ReplyDelete