Image of how the script looks when creating an account record in Dynamics 365 with a primary contact

How to create an account with a primary contact with JavaScript in Dynamics 365

If you think your friends/network would find this useful, please share it with them – I’d really appreciate it.

To create a record in Dynamics with Xrm.WebApi the syntax below can be used. In the example below we create an account record with multiple fields about the account and a reference to a primary contact of the account.

let data = {
    "name": "Litware Publishing",
    "description": "An example account created with Xrm WebApi.",
    "telephone1": "+09-70-01-90-90",
    "address1_line1": "39, quai du Roosevelt",
    "address1_city": "Paris",
    "address1_postalcode": "address1_postalcode",
    "address1_country": "France",
    "primarycontactid@odata.bind": "/contacts(53a0e5b9-88df-e311-b8e5-6c3be5a8b200)",
}

// Create account record
await Xrm.WebApi.createRecord("account", data).then(
    function success(result) {
        console.log("Account created with ID: " + result.id);
    },
    function (error) {
        console.log(error.message);
    }
);

In the image below it show how it looks when running the script in the developer console in the browser.

Image of how the script looks when creating an account record in Dynamics 365 with a primary contact.

Hope this was useful!