web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Deep insert using the web api to create Primary Contact from account doesn't associate account to the contact on the company name field

(0) ShareShare
ReportReport
Posted on by 2,175

Hi all,

So I've been reading this the web API documentation for creating a record using the web API. One of the amazing features is the deep insert feature. For example in one request I can create the account and the primary contact in one go as per below's link:

https://learn.microsoft.com/en-us/power-apps/developer/data-platform/webapi/create-entity-web-api#create-related-table-rows-in-one-operation

I ran this Web API request and it works fine in the sense that the account is created along with a primary contact associated to the account on the Primary Contact lookup field on the account.

The problem is that against the contact record. There's a Company Name field which is a lookup to the account. This does not auto-populate to the account. My question is whether we can do both a deepinsert of account and primary contact as well as associating the created account to the contact or does this require multiple requests and callbacks.

pastedimage1664261513765v1.png

I have the same question (0)
  • Verified answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    It depends on your query, because primarycontactid (on account) and parentcustomerid (on contact) are two separate relationships, one is N:1 and the other is 1:N.

    I honestly don't know if you can fill both relationships with a new record with deep insert.

    hope it helps

  • MikeC282 Profile Picture
    2,175 on at
    [quote user="Guido Preite"]

    It depends on your query, because primarycontactid (on account) and parentcustomerid (on contact) are two separate relationships, one is N:1 and the other is 1:N.

    I honestly don't know if you can fill both relationships with a new record with deep insert.

    hope it helps

    [/quote]

    Thanks bud. I don't think a deep insert is allowed for this cause the account has to be created first before I can associate it with the contact which is basically what the API call is doing so I can't run it in one request. Just tried it and it doesn't work.

    I guess I'll have to run a request first to create the account and primary contact using a deep insert and then run a second request to associate the created account to the contact's parentcustomerid field. Damn...

  • Suggested answer
    Ray Profile Picture
    1,537 on at

    If you can generate guid and use it in deep insert, then you should be about to set both lookup fields. But this is not best pratice.

    var accountId = "00000000-0000-0000-0000-000000000009"
    var contactId = "00000000-0000-0000-0000-000000000010"

    var record = {};
    record = {
    "accountid": accountId,
    "name": "Sample Account",
    "primarycontactid":
    {
    "firstname": "John",
    "lastname": "Smith",
    "contactid": contactId,
    "parentcustomerid_account@odata.bind": "/accounts(" + accountId + ")"
    }
    }

    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.2/accounts", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Prefer", "odata.include-annotations=*");
    req.onreadystatechange = function () {
    if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (this.status === 204) {
    var uri = req.getResponseHeader("OData-EntityId");
    var regExp = /\(([^)]+)\)/;
    var matches = regExp.exec(uri);
    var newId = matches[1];
    console.log(newId);
    } else {
    console.log(this.responseText);
    }
    }
    };
    req.send(JSON.stringify(record));

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 184 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 125

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans