web
You’re offline. This is a read only version of the page.
close
Skip to main content
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,171

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,084 Moderator on at
    RE: Deep insert using the web api to create Primary Contact from account doesn't associate account to the contact on the company name field

    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,171 on at
    RE: Deep insert using the web api to create Primary Contact from account doesn't associate account to the contact on the company name field
    [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
    RE: Deep insert using the web api to create Primary Contact from account doesn't associate account to the contact on the company name field

    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 235

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 175

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 156 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans