Introduction: In this blog, you’ll learn how to use Xrm.WebApi to create records in D365 CE(CRM) or Model-driven Apps.
Use Case: In my example, I have created a custom button named “clone” on the custom table whenever use will click this button duplicate record should get created using Xrm.WebApi.
Implementation
Syntax:
Xrm.WebApi.createRecord(entityLogicalName, data).then(successCallback, errorCallback);
Code:
var webinarFormCustomization = {
cloneRecord: function (exeContext) {
var formContext = exeContext;
//Get the value of all the columns of the current record
var title = formContext.getAttribute("avt_title").getValue(); //Single Line of Text
var description = formContext.getAttribute("avt_description").getValue(); //Multiple Lines of Text
var interested = formContext.getAttribute("avt_interested").getValue(); //Two Option
var country = formContext.getAttribute("avt_country").getValue(); //Option Set(choice)
var age = formContext.getAttribute("avt_age").getValue(); //Whole Number
var topic = formContext.getAttribute("avt_topic").getValue(); //Multi select Option Set(Choices)
var date = formContext.getAttribute("avt_date").getValue(); //Date
var amount = formContext.getAttribute("avt_amount").getValue(); //Currency
var fpn = formContext.getAttribute("avt_fpn").getValue(); //Floating Point Number
var dn = formContext.getAttribute("avt_dn").getValue(); //Decimal Number
//Create object
var webinarObj = new Object();
webinarObj.avt_title = title;
webinarObj.avt_description = description;
webinarObj.avt_interested = interested;
webinarObj["avt_Host@odata.bind"] = "/contacts(5ba17064-1ae7-e611-80f4-e0071b661f01)"; //lookup data type column
webinarObj.avt_country = country;
webinarObj.avt_age = age;
if (topic != null) {
webinarObj.avt_topic = topic.join(); //comma separated string values
}
webinarObj.avt_date = date;
webinarObj.avt_amount = amount;
webinarObj.avt_fpn = fpn;
webinarObj.avt_dn = dn;
webinarObj["avt_Customer_account@odata.bind"] = "/accounts(e44d8589-56ae-eb11-8236-000d3af2817c)";// Customer data type column
// create record
Xrm.WebApi.createRecord("avt_webinar", webinarObj).then(
function success(result) {
Xrm.Utility.alertDialog("Record Cloned Successfully!: " + result.id);
// perform operations on record creation
},
function (error) {
Xrm.Utility.alertDialog(error.message);
// handle error conditions
}
);
},
}
- Call the “cloneRecord” function on the click of a button.

Code Explanation:
I am retrieving column values of the current record and passing them as it is to create a clone(duplicate) record.
Points to be Noted:
- For the Multi-select Option set, we have to pass comma separated string values instead of an array
- For lookup type field we have to pass like below:
fieldSchemaName@odata.bind = "/pluralNameOfTable(<Guid of record>)"
Example:
"avt_Host@odata.bind" = "/contacts(5ba17064-1ae7-e611-80f4-e0071b661f01)";
- For Customer type field we have to pass like below:
//For setting account value in customer type field
fieldSchemaName_account@odata.bind = "/pluralNameOfTable(<Guid of record>)"
Example:
"avt_Customer_account@odata.bind" = "/accounts(e44d8589-56ae-eb11-8236-000d3af2817c)";
Testing:



Sample Code with hard coded values to Create New Record using Xrm.WebApi
var webinarFormCustomization = {
cloneRecord: function (exeContext) {
var webinarObj = new Object();
webinarObj.avt_title = "Creating Clone Record"; //Single line of Text
webinarObj.avt_description = "This is sample record created for testing"; //Multiple line of text
webinarObj.avt_interested = true; //Two Option
webinarObj["avt_Host@odata.bind"] = "/contacts(5ba17064-1ae7-e611-80f4-e0071b661f01)"; //lookup data type column
webinarObj.avt_country = "915350000"; //Option Set(Choice)
webinarObj.avt_age = 25; //Whole Number
webinarObj.avt_topic = "915350000,915350001,915350002"; //Multi Select Option Set(Choices)
webinarObj.avt_date = "2021-05-29"; //Date
webinarObj.avt_amount = 5000; //Currency
webinarObj.avt_fpn = 0.00567; //Floating Point Number
webinarObj.avt_dn = 45.57; //Decimal Number
webinarObj["avt_Customer_account@odata.bind"] = "/accounts(e44d8589-56ae-eb11-8236-000d3af2817c)"; // Customer data type column
// create record
Xrm.WebApi.createRecord("avt_webinar", webinarObj).then(
function success(result) {
Xrm.Utility.alertDialog("Record Cloned Successfully!: " + result.id);
// perform operations on record creation
},
function (error) {
Xrm.Utility.alertDialog(error.message);
// handle error conditions
}
);
},
}