How to Create a Record Synchronously in D365 CRM and Pass the Created Record ID in the Second Function using JavaScript

Create Record Synchronously in D365 CRM with JSCreate Record Synchronously in D365 CRM with JS

Creating a record and using its ID in subsequent operations is a common task in various applications, including Microsoft Dynamics 365. This article will guide you through the process of creating a record synchronously and then passing the created record’s ID to a second function using JavaScript and the Dynamics 365 Web API.

Understanding the Basics

Before diving into the implementation, it’s important to understand the basic concepts:

  • Synchronous Operation: This means the operation is completed before moving on to the next step.
  • Record ID: A unique identifier for a record in the database. In Dynamics 365, this is typically a GUID (Globally Unique Identifier).


  • Basic knowledge of JavaScript.
  • Access to a Dynamics 365 instance with appropriate permissions to create records.
  • Familiarity with the Dynamics 365 Web API.

Step-by-Step Guide

1. Set Up the Function to Create the Record

First, we need a function that will create a record in Dynamics 365. This function will make an HTTP POST request to the Dynamics 365 Web API.

function createRecord(entityName, data, successCallback, errorCallback) {
    var req = new XMLHttpRequest();"POST", Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.1/" + entityName, true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "return=representation");

    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 201) {
                var createdRecord = JSON.parse(this.response);
            } else {

In this function:

  • entityName is the name of the entity you want to create (e.g., contacts).
  • data is the JSON object representing the record to be created.
  • successCallback is the function to call upon the successful creation of the record.
  • errorCallback is the function to call if an error occurs.

2. Define the Second Function

Next, define the function that will use the created record’s ID.

function secondFunction(recordId) {
    console.log("Record ID passed to the second function: " + recordId);
    // Perform additional operations using the record ID

3. Implement the Main Logic

Finally, create a main function that coordinates the record creation and the subsequent operation.

function mainFunction() {
var entityName = “contacts”; // The entity name for Contact
var data = {
firstname: “John”,
lastname: “Doe”,
emailaddress1: “”

createRecord(entityName, data, function(createdRecord) {
    var createdRecordId = createdRecord.contactid;  // Assuming the entity is 'contacts'
}, function(error) {
    console.error("Error creating record: " + error);


Detailed Explanation

  1. Creating the Record:
    • The createRecord function initializes an XMLHttpRequest to send a POST request to the Dynamics 365 Web API to create a new record.
    • The headers are set to specify the JSON format and API version.
    • The Prefer header with return=representation ensures the created record is returned in the response.
    • On a successful request (status === 201), the response containing the created record’s details is parsed, and the success callback is called with this data.

  2. Handling the Created Record:
    • In the mainFunction, the createRecord function is called with the required entity data.
    • Upon successful creation, the ID of the created record is extracted (e.g., createdRecord.contactid for the Contact entity).
    • This ID is then passed to the secondFunction for further processing.

By following these steps, you can create a record synchronously in Dynamics 365 and pass the created record’s ID to another function for further operations. This approach ensures that your application logic remains orderly and that subsequent operations have access to the necessary data. This technique is especially useful in scenarios where actions need to be performed sequentially based on the creation of new records.

Leave a Reply

Your email address will not be published. Required fields are marked *