Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Prevent record deactivation if child records are active

Posted on by 390

Is it possible to prevent a user from deactivating a record if there are related records still active e.g. appointment, tasks etc?

An out of the box method would be best, or perhaps a custom method?

Thanks.

*This post is locked for comments

  • Temmy Wahyu Raharjo Profile Picture
    Temmy Wahyu Raharjo 2,914 on at
    RE: Prevent record deactivation if child records are active

    Maybe you can make a global class to achieve that. Something like this :

    public void CheckActivate(IOrganizationService service, string entityName, string attributeName, Guid relatedId)

    {

    var query = new QueryExpression(entityName){

    ColumnSet = new ColumnSet(false),

    TopCount = 1

    };

    query.Criteria.AddCondition(attributeName, ConditionOperator.Equal, relatedId);

    query.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);

    var result = service.RetrieveMultiple(query);

    if(result.Entities.Any()) throw new InvalidPluginExecutionException("There is active related record");

    }

    You can add in the plugin and define all the related. Or in more advance code, maybe you can query the related attribute and loop all of the information needed.

    Please forgive me if the code not work. Just pseudo code this. But the idea is something like that.

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Prevent record deactivation if child records are active

    You can do this using a plugin that will run on the SetStateDynamicEntity message of the record that you are looking to deactivate.

    In the Pre-Operation message of the message, check and see if any of the child messages have a status of Active (You can do an aggregate query with a condition of StateCode = 0 to get the total count of active record. If the result is more than 0, you know that you want to cancel, and you basically throw an exception.

    This will prevent the record from being deactivated.

    Hope this helps.

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Prevent record deactivation if child records are active

    Hi,

    There is no out ob box method to achieve this. You need to write something custom.

    You can refer the below thread which talks about counting the child records. You can change it count only the active child records and then use it in a workflow to prevent the parent state change.

    community.dynamics.com/.../147466

    Hope this helps.

  • TomSmelser Profile Picture
    TomSmelser 1,570 on at
    RE: Prevent record deactivation if child records are active

    I don't think there is out of the box method. Other ways you could handle this. 1 custom plugin/workflow activity.  2 javascript.

  • 2015crm Profile Picture
    2015crm 390 on at
    RE: Prevent record deactivation if child records are active

    You are talking about setting the status to cascade, thats not quite what im after here.

    I need to PREVENT the deactivation of a record, if there are related records that are still active. I dont need to have the status cascade down.

    So the scenario would be, the user would attempt to deactivate a record, it would error and say "unable to deactivate this record as there are open activities associated to this record"

  • Suggested answer
    TheMarkChristie Profile Picture
    TheMarkChristie 10,328 on at
    RE: Prevent record deactivation if child records are active

    It looks like you need to set the relationship to Referential, Restrict Delete

    Here are the options that are available, but keep in mind that not all types are available for all relationships in the system.

    Type of Behavior

    Parental: this forces the Cascade All effect to all the individual behaviors

    Referential: this forces the Cascade None effect to all the individual behaviors except Delete which is locked to Remove Link

    Referential, Restrict Delete: this forces the Cascade None effect to all the individual behaviors except Delete which is locked to Restrict

    Configurable Cascading: this allows you to define the individual behaviors independently

    Individual Behaviors

    Assign – the record owner changes

    Share  â€“ the record is shared with another user

    Unshare – the record is unshared with another user

    Reparent – the referencing attribute of a parental relationship is changed

    Delete – the record is deleted

    Merge – the record is merged with another record

    For Assign, Share, Unshare, and Reparent the options are:

    Cascade All – the change is applied to all associated records

    Cascade Active – the change is applied to all associated records that are active

    Cascade User Owned – the change is applied to all associated records owned by the user who owns the original record

    Cascade None – the change is not applied to any other records

    For Delete the options are:

    Cascade All

    Remove Link – the link between the original and referenced records is removed

    Restrict – prevents the original record from being deleted when related to child records

    For Merge the options are:

    Cascade All

    Cascade None

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans