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