0

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?

2 Answers2

0

If create an new instance of entity it has no id because it was not created in the dynamics until you run. The instance of Entity is located only in your memory.

Guid accountId = this._serviceClient.Create(account)

To link the account with the contact you need the guid of the account. So before you linkt the entities you have to call

Guid accountId = this._serviceClient.Create(account)
arne
  • 123
  • 9
  • That I know. But you still can create multiple entities at once like I explained above. My question is how can I link the "parentcustomerid" with the account when I'm creating the entities. The contact is linked to the account with the "account_contact_AccountId" but I also need to set "parentcustomerid" at the same time. How can I do that? This is my question. – Antoine Brisebois-Roy Aug 10 '21 at 14:28
  • Try ```contact.Attributes.Add("parentcustomerid", new EntityReference(this._options.Account.Name, accountId));``` – arne Aug 11 '21 at 06:58
  • Yes, this would work, but I don't have the accountId since I want to create everything in a single request, hence my question. – Antoine Brisebois-Roy Aug 11 '21 at 16:38
  • As I described above, this does not work, because you get the ID of the company only after it has been created in Dynamics. – arne Aug 12 '21 at 05:39
  • Ok perfect. I thought that there would be a way since we can created the contact at the same time than the account using a relationship and relatedEntities. Thank you for confirming there is no way to link an additional field to account when using only one operation for all the creations. I will continue to update to parentcustomerId once everything is created each time. – Antoine Brisebois-Roy Aug 12 '21 at 13:04
0

You have to generate some Guid's yourself, but if you do that, it's possible.

See the sample from Microsoft: https://github.com/microsoft/PowerApps-Samples/blob/1adb4891a312555a2c36cfe7b99c0a225a934a0d/cds/orgsvc/C%23/CreateUpdateRecordsWithRelatedRecords/CreateUpdateRecordsWithRelatedRecords/SampleProgram.cs

gnud
  • 77,584
  • 5
  • 64
  • 78