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 :
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!
I have the same question (0)
  • Suggested answer
    InFlowEncer89 Profile Picture
    35 on at
    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. 
     
     
  • BeauTP Profile Picture
    8 on at
    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
    Cui Hao Profile Picture
    Microsoft Employee on at
    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

    ​​​​​​​
  • MichaelVanDijk Profile Picture
    12 on at
    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.

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 April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 128 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 119

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans