Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Plugin to create a child record and also update the parent record with the Child's ID.

(0) ShareShare
ReportReport
Posted on by 332

I have a requirement to create new contact every time an account is being created.

However the account must also be updated with the contact as Primary account.

Everything works fine and the contact is created but I get an error to update the account .

The GUID of the contact returned is 00000000-0000-0000-0000-000000000000.

have registered the plugin also in asynchronous mode, but the primary contact is not update on the account.

The plugin is registered on the PostOperation of create account.

I know this can be done easily with a workflow but the requirement is to use a plugin.

try
{
  Entity postImage = (Entity)context.PostEntityImages["PostImage"];
  var accountID = postImage.GetAttributeValue<Guid>("accountid");
  var companyName = postImage.GetAttributeValue<String>("name");
  var companyPhone = postImage.GetAttributeValue<String>("telephone1");

 Entity contact = new Entity("contact");
 contact["firstname"] = companyName;
 contact["lastname"] = "Account";
 contact["telephone1"] = companyPhone;
 contact["parentcustomerid"] = new EntityReference("account", accountID);

 IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
 IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

 tracingService.Trace("Plugin: Creating the Contact.");
 service.Create(contact);

 Guid contactid = contact.Id;
 tracingService.Trace("The Contact ID is " +contactid); //checking the contact ID but it returns 000000000000000


  Entity account = new Entity("account", accountID);
  account["primarycontactid"] = new EntityReference("contact", contactid);
  service.Update(account); // update the primary contact lookup
}


*This post is locked for comments

  • lawix10 Profile Picture
    lawix10 332 on at
    RE: Plugin to create a child record and also update the parent record with the Child's ID.

    Spot ON

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Plugin to create a child record and also update the parent record with the Child's ID.

    Instead of the following lines:

    service.Create(contact);

    Guid contactid = contact.Id;

    Change it to:

    Guid contactid = service.Create(contact);

    The reason is that the id of the contact is returned from the Create method, but the contact entity does not have an id when you submit it for creation, which is why you are getting an empty Guid.

    Hope this helps.

  • Verified answer
    Ivan Ficko Profile Picture
    Ivan Ficko 1,380 on at
    RE: Plugin to create a child record and also update the parent record with the Child's ID.

    service.Create() method is returning the GUID of created entity. You need to store the return value of that function to a variable to retrieve that GUID.

    var contactId = service.Create(contact);

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,307 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans