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
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.
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.
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.
I don't think there is out of the box method. Other ways you could handle this. 1 custom plugin/workflow activity. 2 javascript.
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"
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
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156