function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Muthu SamyMuthu Samy 

Custom case validation error using trigger

Can somebody help me on setting a custom validation using trigger on case object ? 

Want to restrict users adding more than 500 children cases to parent case.
Praveen KHariPraveen KHari
Below code may work.... don't forget to add your error 
trigger ChildCaseValidationTrigger on Case (before insert) {

    List<Case> caseList = trigger.new;
    set<Id> ParentIds = new set<Id> ();
    
    for(Case cs : caseList){
        ParentIds.add(cs.ParentId);
    }
    
    if(!ParentIds.isEmpty()){
        AggregateResult[] childCaseCounts = [SELECT ParentId, Count(Id) FROM Case Where ParentId=:ParentIds GROUP BY ParentId];
        Map<Id,Integer> chilCountByParentIdMap = new Map<Id, Integer>();
        for(AggregateResult ar : childCaseCounts )  {
            chilCountByParentIdMap.put( Id.ValueOf(string.valueOf(ar.get('ParentId'))), Integer.valueOf(ar.get('expr0')) );
        }
        
        for(Case cs : caseList){
            Id ParentId = cs.ParentId;
            if(ParentId != null){
               Integer count =  chilCountByParentIdMap.get(ParentId);
               if(count != null && count > 500){
                   //Add your error
               }
            }
        }
    }
JethaJetha
Above code is going to work fine.............
Praveen KHariPraveen KHari
Appreciate choosing best answer if the code fix your issue.
 
trigger ChildCaseValidationTrigger on Case (before insert) {

    List<Case> caseList = trigger.new;
    set<Id> ParentIds = new set<Id> ();
    
    for(Case cs : caseList){
        ParentIds.add(cs.ParentId);
    }
    
    if(!ParentIds.isEmpty()){
        AggregateResult[] childCaseCounts = [SELECT ParentId, Count(Id) FROM Case Where ParentId=:ParentIds GROUP BY ParentId];
        Map<Id,Integer> chilCountByParentIdMap = new Map<Id, Integer>();
        for(AggregateResult ar : childCaseCounts )  {
            chilCountByParentIdMap.put( Id.ValueOf(string.valueOf(ar.get('ParentId'))), Integer.valueOf(ar.get('expr0')) );
        }
        
        for(Case cs : caseList){
            Id ParentId = cs.ParentId;
            if(ParentId != null){
               Integer count =  chilCountByParentIdMap.get(ParentId);
               if(count != null && count > 500){
                   cs.addError('500 or more child on selected parent case');
               }
            }
        }
    }
}



-Praveen K Hari
Muthu SamyMuthu Samy
Praveen, Thanks for the response. I'm testing this now. I think it will work only on insert. how about update event? 
Praveen KHariPraveen KHari
Hi Muthu,

Update will be little more complex. You have to do the validation only if the parent id is changed. You can use Trigger.oldMap to get the old values and compare with Trigge.new variable parent id. If you are finding a different in the parent id take the Trigger.new parent id and check its count as we are doing in above code.

You can use my code for the reference and build update event also.

-Praveen K Hari