Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Deactivate contacts - best practice

(1) ShareShare
ReportReport
Posted on by 12
Hello everyone,
 
We're struggling in our organization with the deactivation of contacts.
 
Since last year we've started to use cases, leads and opportunities. This has caused an influx of new contacts in Dynamics 365, which is great. Our problem is that when one department wants to deactivate a contact, they have to check if this contact is still connected to leads, opportunities, our website login, cases, etc.
 
What would be a solution for us to make this check a bit easier? Now they have to look on different tabs and sections to see if this contact can actually be deactivated. Is there any way to get a warning during or before deactivating the contact that it's still connected to active records?
 
Thanks in advance!
  • MichaelVanDijk Profile Picture
    12 on at
    Deactivate contacts - best practice
    Thanks for your replies!
     
    I think I am going to go with a workflow then to do these checks, and possibly also adding a tab with the connected records. I'd prefer to go low-code in our solution.
  • Suggested answer
    Cui Hao Profile Picture
    on at
    Deactivate contacts - best practice
    Hi,

    For your question, I have analyzed and tested.
    You might be able to write plugins to do what you want.
    I wrote you a sample code that fails to Deactivate when contact is still referenced by lead.

    The code is as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Query;

     
    namespace Plugins
    {
        public class PrevalidationContact : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                //Preliminary work
                ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                //==================================================================

     
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    try
                    {
                        Entity contact = (Entity)context.InputParameters["Target"];
                        var contactId = contact.Id;
                        OptionSetValue status = contact.GetAttributeValue<OptionSetValue>("statecode");

     
                        //If the Stauts field is set to 0 (meaning the contact is activated), exit the program.
                        if (status.Value == 0&&status ==null)
                        {
                            return;
                        }

     
                        //Query lead records.
                        QueryExpression query = new QueryExpression("lead");

     
                        query.Criteria.AddCondition("parentcontactid", ConditionOperator.Equal, contactId);
     
                        EntityCollection entityCollection = service.RetrieveMultiple(query);
     
                        //If the number of lead records returned by the query is greater than 0, the contact will not be deactivated and an error will be reported.
                        if (entityCollection.Entities.Count > 0)
                        {
                            throw new InvalidPluginExecutionException("This contact cannot be deactivated because it is referenced by a lead!");
                        }
                    }
                    catch (Exception ex)
                    {
                        tracingService.Trace(ex.Message);
                        throw new InvalidPluginExecutionException(ex.Message);
                    }

                }
            }
        }
    }




    If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more. If you have further questions, please feel free to contact me.


    Best regards,
    Cui Hao

    ​​​​​​​
  • BeauTP Profile Picture
    8 on at
    Deactivate contacts - best practice
    InFlow has a great customization option which I've personally implemented many times. If you want to avoid code or a power automate flow you could always create a tab on the form shows all your relationships (lots of sub grids :-D) to give the person one space to look when they are deactivating. 
  • Suggested answer
    InFlowEncer89 Profile Picture
    35 on at
    Deactivate contacts - best practice
    Hello, 
     
    This is possible, but does require customisation. 
     
    I have done this for a customer using a custom workflow activity so the steps will check the child relationships and you can then query the result to say if the count >1 then stop workflow with an error (you can specify the error context too). 
     
    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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,135 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,871 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans