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 :
Microsoft Dynamics CRM (Archived)

Process for ParentCustomerID handling

(0) ShareShare
ReportReport
Posted on by

Dear all,

does anybody have an idea how to handle the following Situation:

System CRM 365 with a lot of Customization

My Customer would like to Automatize the following:

If the Primary Contact is manually removed in the Account Entity the ParentCustomerID field on the contact should also be cleared, meaning that the Account Name should be deleted from there without the Need to step into the contact and remove it there.

Does anybody have an good and simple idea how to do that?

Thank you 

Best Regards

Richard

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Hi Richard,

    You can create a workflow for this, please check below link for creating workflow,

    blogs.msdn.microsoft.com/.../use-workflow-to-set-the-primary-contact

    crmbook.powerobjects.com/.../workflows

    best regards,

    Shahbaaz

  • Community Member Profile Picture
    on at

    Dear Shabaaz,

    thank you for the Suggestion, but how to set is clear, this is already in place.

    I would Need: how to delete (remoove) from Account

    Any idea on this?

    Thank you

    Best Regards

    Richard

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Hi Richard,

    I tried with workflow but its look like it does not support the child entity update using workflow,

    For this you can create a plugin on Account  on Update Message using Post Operation and using Asynchronous(Which will run in background)

    Note : Also register Pre-image with the name "RemovedContact" and in Field only select Primary Contact

    Right click on the step added-->add image-->select preimage and in that select primary contact field


    Below is the full code, just you need to make sure you pass the schema name of field properly,

    ***********************************************************************

    using System;
    using System.Collections.Generic;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Messages;

    namespace Account.Plugin
    {
    public class RemoveAccountFromContact : IPlugin
    {

    public void Execute(IServiceProvider serviceProvider)
    {

    //Extract the tracing service for use in debugging sandboxed plug-ins.
    ITracingService tracingService =
    (ITracingService)serviceProvider.GetService(typeof(ITracingService));

    // Obtain the execution context from the service provider.
    IPluginExecutionContext context = (IPluginExecutionContext)
    serviceProvider.GetService(typeof(IPluginExecutionContext));


    // The InputParameters collection contains all the data passed in the message request.
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
    // Obtain the target entity from the input parameters.
    Entity entity = (Entity)context.InputParameters["Target"];

    // Verify that the target entity represents an account.
    // If not, this plug-in was not registered correctly.
    if (entity.LogicalName != "account")
    return;

    try
    {

    // Obtain the organization service reference.
    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

    //check if Primary contact is removed from account
    if (!entity.Attributes.Contains("primarycontactid"))
    {
    //get the primary contact value using pre image
    Entity RemovedContact = (Entity)context.PreEntityImages["RemovedContact"];
    // check if primary contact from pre image contain data
    if (RemovedContact.Attributes.Contains("primarycontactid"))
    {
    // get the Guid of the primary contact
    Guid precontactGuid = ((EntityReference)RemovedContact.Attributes["primarycontactid"]).Id;

    //Remove account lookup value from primary contact record
    Entity contact = new Entity("contact");
    contact.Id = precontactGuid;
    contact["parentaccountid"] = null;
    service.Update(contact);
    }
    }

    }
    catch (Exception ex)
    {
    tracingService.Trace("An error occurred in the FollowupPlugin plug-in : " + ex);
    //throw new InvalidPluginExecutionException("An error occurred in the FollowupPlugin plug-in.", ex);
    }
    }
    }
    }
    }

    ***********************************************************************

    If you find any issue, Please let me know.

    If you find it helpful, Please mark as Verified.

    Best Regards,

    Shahbaaz

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Richard,

    You won't be able to achieve this using standard workflows. You would need to right custom code. You can right a plugin with pre image and compare the previous value and new value. Based on this you can clear the account name from that contact.

    The above sample should work provided you registered the pre image as well with the name "RemovedContact".

    Hope this helps.

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 > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans