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); }