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 !




    

       

24 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
  14. Datastage learning
    datastage learning is a valuable step for anyone looking to enter the world of data integration and ETL. The tool offers powerful features that are well worth mastering. I find the learning process engaging, especially with hands-on exercises. It’s a great skill to have in today’s data-driven industry.

    ReplyDelete
  15. The Salesforce CPQ course provides in-depth knowledge of configuring and automating the Configure, Price, Quote process. Learn to leverage Salesforce’s tools to boost your productivity and enhance customer satisfaction.salesforce cpq course

    ReplyDelete
  16. Very informative! MuleSoft training India is growing rapidly with increasing job opportunities.MuleSoft Training India

    ReplyDelete
  17. Great insights shared here. Mule software training at OnlineITGuru provides hands-on exposure to enterprise integration scenarios and real-time implementations.mule software training

    ReplyDelete
  18. Great article! The Salesforce CPQ training course from OnlineITGuru is well-designed and includes hands-on exercises that are very helpful.salesforce cpq training course

    ReplyDelete
  19. Great post! Our dell boomi course
    helps learners master cloud integration, API management, and workflow automation with hands-on projects to prepare for real-world IT roles.

    ReplyDelete
  20. Salesforce Developer Course
    Salesforce developer course is a good career option in India. This developer course india looks informative. Simple structure. Useful learning.

    ReplyDelete
  21. ⭐ Online BA Analyst Course India
    A career-focused online ba analyst course india helps learners gain industry-relevant skills.
    The course covers business analysis tools and techniques.
    Hands-on projects provide practical exposure.
    Expert trainers guide students step by step.
    Assignments help strengthen analytical abilities.
    Flexible schedules make learning easy.
    Interactive sessions improve understanding.
    The curriculum is aligned with Indian industry standards.
    Certification guidance enhances job opportunities.
    This course is ideal for building a strong BA career in India.

    ReplyDelete
  22. An iOS mobile app development course teaches learners how to design and develop applications for Apple platforms. It explains programming logic, UI components, and app lifecycle clearly. This ios mobile app development course helps students gain hands-on experience through projects and assignments. Learners build real applications. The course prepares learners for careers in mobile app development.

    ReplyDelete
  23. The content is clear and useful for anyone focusing on uipath learning from scratch.

    ReplyDelete
  24. This post is a great resource for professionals who want to learn ui path step by step.

    ReplyDelete