{"id":37519,"date":"2024-06-06T17:46:45","date_gmt":"2024-06-06T12:16:45","guid":{"rendered":"https:\/\/techmasala.addastudents.com\/dev\/?p=37519"},"modified":"2024-06-06T17:46:47","modified_gmt":"2024-06-06T12:16:47","slug":"how-to-create-a-record-synchronously-in-d365-crm-and-pass-the-created-record-id-in-the-second-function-using-javascript","status":"publish","type":"post","link":"https:\/\/techmasala.addastudents.com\/dev\/how-to-create-a-record-synchronously-in-d365-crm-and-pass-the-created-record-id-in-the-second-function-using-javascript\/","title":{"rendered":"How to Create a Record Synchronously in D365 CRM and Pass the Created Record ID in the Second Function using JavaScript"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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&#8217;s ID to a second function using JavaScript and the Dynamics 365 Web API.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding the Basics<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before diving into the implementation, it&#8217;s important to understand the basic concepts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Synchronous Operation<\/strong>: This means the operation is completed before moving on to the next step.<\/li>\n\n\n\n<li><strong>Record ID<\/strong>: A unique identifier for a record in the database. In Dynamics 365, this is typically a GUID (Globally Unique Identifier).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Basic knowledge of JavaScript.<\/li>\n\n\n\n<li>Access to a Dynamics 365 instance with appropriate permissions to create records.<\/li>\n\n\n\n<li>Familiarity with the Dynamics 365 Web API.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Step-by-Step Guide<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Set Up the Function to Create the Record<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function createRecord(entityName, data, successCallback, errorCallback) {\n    var req = new XMLHttpRequest();\n    req.open(\"POST\", Xrm.Utility.getGlobalContext().getClientUrl() + \"\/api\/data\/v9.1\/\" + entityName, true);\n    req.setRequestHeader(\"OData-MaxVersion\", \"4.0\");\n    req.setRequestHeader(\"OData-Version\", \"4.0\");\n    req.setRequestHeader(\"Accept\", \"application\/json\");\n    req.setRequestHeader(\"Content-Type\", \"application\/json; charset=utf-8\");\n    req.setRequestHeader(\"Prefer\", \"return=representation\");\n\n    req.onreadystatechange = function() {\n        if (this.readyState === 4) {\n            req.onreadystatechange = null;\n            if (this.status === 201) {\n                var createdRecord = JSON.parse(this.response);\n                successCallback(createdRecord);\n            } else {\n                errorCallback(this.responseText);\n            }\n        }\n    };\n    req.send(JSON.stringify(data));\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this function:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>entityName<\/code> is the name of the entity you want to create (e.g., <code>contacts<\/code>).<\/li>\n\n\n\n<li><code>data<\/code> is the JSON object representing the record to be created.<\/li>\n\n\n\n<li><code>successCallback<\/code> is the function to call upon the successful creation of the record.<\/li>\n\n\n\n<li><code>errorCallback<\/code> is the function to call if an error occurs.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. Define the Second Function<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Next, define the function that will use the created record&#8217;s ID.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function secondFunction(recordId) {\n    console.log(\"Record ID passed to the second function: \" + recordId);\n    \/\/ Perform additional operations using the record ID\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Implement the Main Logic<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, create a main function that coordinates the record creation and the subsequent operation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">function mainFunction() {<br>var entityName = &#8220;contacts&#8221;; \/\/ The entity name for Contact<br>var data = {<br>firstname: &#8220;John&#8221;,<br>lastname: &#8220;Doe&#8221;,<br>emailaddress1: &#8220;john.doe@example.com&#8221;<br>};<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>createRecord(entityName, data, function(createdRecord) {\n    var createdRecordId = createdRecord.contactid;  \/\/ Assuming the entity is 'contacts'\n    secondFunction(createdRecordId);\n}, function(error) {\n    console.error(\"Error creating record: \" + error);\n});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Detailed Explanation<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Creating the Record<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>createRecord<\/code> function initializes an XMLHttpRequest to send a POST request to the Dynamics 365 Web API to create a new record.<\/li>\n\n\n\n<li>The headers are set to specify the JSON format and API version.<\/li>\n\n\n\n<li>The <code>Prefer<\/code> header with <code>return=representation<\/code> ensures the created record is returned in the response.<\/li>\n\n\n\n<li>On a successful request (<code>status === 201<\/code>), the response containing the created record&#8217;s details is parsed, and the success callback is called with this data.<br><br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Handling the Created Record<\/strong>:\n<ul class=\"wp-block-list\">\n<li>In the <code>mainFunction<\/code>, the <code>createRecord<\/code> function is called with the required entity data.<\/li>\n\n\n\n<li>Upon successful creation, the ID of the created record is extracted (e.g., <code>createdRecord.contactid<\/code> for the Contact entity).<\/li>\n\n\n\n<li>This ID is then passed to the <code>secondFunction<\/code> for further processing.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">By following these steps, you can create a record synchronously in Dynamics 365 and pass the created record&#8217;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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s ID to a second function using JavaScript and the Dynamics 365 Web API. Understanding the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":37523,"comment_status":"open","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[71,70,17,72],"tags":[915,917,918,45,916],"class_list":["post-37519","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-learn-javascript-online","category-learn-technology-free","category-learn-microsoft-dynamics-crm","category-tech","tag-create","tag-d365-crm","tag-http","tag-js","tag-synchronously"],"_links":{"self":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts\/37519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/comments?post=37519"}],"version-history":[{"count":3,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts\/37519\/revisions"}],"predecessor-version":[{"id":37522,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/posts\/37519\/revisions\/37522"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/media\/37523"}],"wp:attachment":[{"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/media?parent=37519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/categories?post=37519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techmasala.addastudents.com\/dev\/wp-json\/wp\/v2\/tags?post=37519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}