Dynamics 365 CRM REST Snippets
REst is an important protocol to interact with CRM objects through javascript. Here I will discuss how to start REST codings using Dynamics CRM.
Pre-requisites:
- Download the CRM SDK for your CRM version from here :Â Download SDK Here (Imp: Choose your correct version of SDK)
- Once you downloaded the SDK extract the package and you will see the below directory structure after extraction.
Now navigate the below path and you will get 3 java script files as per below screenshot.
Path
C:\Users\Sanjaya\Desktop\Dynamics CRM SDK 2011\SDK\SampleCode\JS\RESTEndpoint\JavaScriptRESTDataOperations\JavaScriptRESTDataOperations\Scripts
We only require two scripts json2.js & SDK.REST from this folder.
You can refer this article for more information on REstÂ
- Now create two JScript web resources in Dynamics CRM and upload these above two javascript in the web resources.
- Create another JScript web resource (FormScript_Account_REST.JS) to add our Rest CRUD functions.
- This example targets Account entity.
- Follow the below examples to take idea how to write REst code and change your code accordingly.
- Now add these 3 scripts on the form editing library of contact entity form.
- The Libraries order should be.
- json2.js
- SDK.REST.js
- FormScript_Account_REST.js
- Add events required and point to the function written in FormScript_Account_REST.js script.
Sample Codes:
For parameters details info read this article.
CREATE RECORD
function createAccount() { var account = {}; account.Name = "Test Account Name"; account.Description = "This account was created by REst."; //Create the Account SDK.REST.createRecord( account, "Account", function (account) { alert("The account named \"" + account.Name + "\" is created"); }, errorHandler ); } function errorHandler(error) { alert(error.message); }
RETRIEVE RECORD
function retrieveAccount(AccountId) { SDK.REST.retrieveRecord( AccountId, "Account", null,null, function (account) { alert("Retrieved the account named \"" + account.Name + "\".); }, errorHandler ); }
function errorHandler(error) { alert(error.message); }
UPDATE RECORD
function updateAccount(AccountId) { var account = {}; alert("Changing the account Name to \"Updated Account Name\"."); account.Name = "Updated Account Name"; alert("Adding Address information"); account.Address1_AddressTypeCode = { Value: 3 }; //Address1: AddressType=Primary account.Address1_City = "Sanjay"; account.Address1_Line1 = "123 High St."; account.Address1_PostalCode = "98074"; account.Address1_StateOrProvince = "WA"; alert("Setting E-Mail address"); account.EMailAddress1 = "me@sh034.global.temp.domains"; SDK.REST.updateRecord( AccountId, account, "Account", function () { alert("The account record changes were saved"); }, errorHandler ); }
function errorHandler(error) { alert(error.message); }
DELETE RECORD
function deleteAccount(AccountId) { alert("You chose to delete the account record."); SDK.REST.deleteRecord( AccountId, "Account", function () { alert("The account is deleted."); }, errorHandler ); }
function errorHandler(error) { alert(error.message); }
Don’t miss the chance to participate in the upcoming Internship Program which will be done using Microsoft Dot Net Web Development Full Stack Technology. The new batch will be starting from May 20, 2024. We will have most experienced trainers for you to successfully complete the internship with live project experience.
Why to choose Our Internship Program?
Industry-Relevant Projects
Tailored Assignments: We offer projects that align with your academic background and career aspirations.
Real-World Challenges: Tackle industry-specific problems and contribute to meaningful projects that make a difference.
Professional Mentorship
Guidance from Experts: Benefit from one-on-one mentorship from seasoned professionals in your field.
Career Development Workshops: Participate in workshops that focus on resume building, interview skills, and career planning.
Networking Opportunities
Connect with Industry Leaders: Build relationships with professionals and expand your professional network.
Peer Interaction: Collaborate with fellow interns and exchange ideas, fostering a supportive and collaborative environment.
Skill Enhancement
Hands-On Experience: Gain practical skills and learn new technologies through project-based learning.
Soft Skills Development: Enhance communication, teamwork, and problem-solving skills essential for career success.
Free Demo Class Available