I'm using the Microsoft.PowerPlatform.Dataverse.Client library to get and create data into my dynamics 365 env using a .Net core app. I'm trying to create an Account with a Contact in the same request using this method.
It works great, but I can't manage a way to link the contact "parentcustomerid" to the Account I just created while still doing only one request.
I found a workaround doing an update after the creation, but it goes against the fact that I only want to do one request (the execution time really needs to stay as low as possible). There is the workaround code:
var account = new Entity(this._options.Account.Name);
account[this._options.Account.Columns.AccountName] = accountName;
var contact = new Entity(this._options.Contact.Name);
contact[this._options.Contact.Columns.FirstName] = dataverseContact.FirstName;
contact[this._options.Contact.Columns.LastName] = dataverseContact.LastName;
contact[this._options.Contact.Columns.Email] = dataverseContact.Email;
var contactCollection = new EntityCollection();
contactCollection.Entities.Add(contact);
account.RelatedEntities[new Relationship("aci_account_contact_AccountId")] = contactCollection;
var accountId = this._serviceClient.Create(account);
var contactId = GetContactIdByEmail(dataverseContact.Email).Result;
var updatedContact = new Entity(this._options.Contact.Name);
updatedContact.Id = contactId;
updatedContact[this._options.Contact.Columns.CompanyName] = new EntityReference(this._options.Account.Name, accountId);
this._serviceClient.UpdateAsync(updatedContact);
However, I would like something like this (which doesn't work since I don't have the accountId before it is created...):
var account = new Entity(this._options.Account.Name);
account[this._options.Account.Columns.AccountName] = accountName;
var contact = new Entity(this._options.Contact.Name);
contact[this._options.Contact.Columns.FirstName] = dataverseContact.FirstName;
contact[this._options.Contact.Columns.LastName] = dataverseContact.LastName;
contact[this._options.Contact.Columns.Email] = dataverseContact.Email;
// New line that is not working because I don't have the accountId
contact["parentcustomerid"] = new EntityReference(this._options.Account.Name, accountId);
var contactCollection = new EntityCollection();
contactCollection.Entities.Add(contact);
account.RelatedEntities[new Relationship("aci_account_contact_AccountId")] = contactCollection;
var accountId = this._serviceClient.Create(account);
I was wondering if there is a solution to this or I'm stuck with doing a Get and then an Update every time?