REST – Create Example

Create

Syntax: SDK.REST.createRecord(object, type, successCallback, errorCallback)

Name Type Description
object Object A JavaScript object with properties corresponding to the Schema name of entity attributes that are valid for create operations.
type String The Schema Name of the Entity type record to create.
successCallback Function The function that will be passed through and be called by a successful response. This function can accept the returned record as a parameter.
errorCallback Function The function that will be passed through and be called by a failed response. This function must accept an Error object as a parameter.

Task:Create a Contact Record

Solution: Add the below Script in “new_sdkOperations” Webresource and click Ok.

Please do check REST – Retrieve Example for Initial Setup

function createAccount() {
var account = {};
account.Name = "Arun Potti";
account.Description = "Account was created using SDK.REST.createRecord Method";

 //Set a lookup value
account.PrimaryContactId = {
Id: "FB4F88B0-49B3-E311-9C70-D89D676E8210", //Provide Existing Contact Guid
LogicalName: "contact",
Name: "Maria Campbell (sample)"                          //Provide Existing Contact Name
};

//Set a picklist value
account.PreferredContactMethodCode = {   Value: 2   };

   //Set a money value
account.Revenue = { Value: "2000000.00"   };

   //Set a Boolean value
account.DoNotPhone = true;

  //Add Two Tasks
var today = new Date();
var startDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 3);   //Set a date three days in the future.

  //Low Priority Task
var LowPriTask = { Subject: "Low Priority Task", ScheduledStart: startDate, PriorityCode: { Value: 0 } };

//High Priority Task
var HighPriTask = { Subject: "High Priority Task", ScheduledStart: startDate, PriorityCode: { Value: 2 } };      account.Account_Tasks = [LowPriTask, HighPriTask];

//Create the Account

SDK.REST.createRecord(account, "Account", getAccountDetails, errorHandler);
}

function getAccountDetails(account) {
alert("Account Name : " + account.Name + "\nAccount GUId : " + account.AccountId);
}

function errorHandler(error) { alert(error.message); }

Add OnLoad event in Account Form, Library: new_sdkOperations &function createAccount. Save and Publish

REST - Create Webresource Output

Output:

REST - Create Output

Please provide your valuable comments on this article.


Discover more from Arun Potti's Power Platform blog

Subscribe to get the latest posts to your email.

Leave a Reply