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
SambitNayakSambitNayak 

check child object sum

Hi,

I have contact object with Team Member as child object.
The child object has a field --- allocation.
My requirement is sum of allocations cant exceed 100%. I tried a validation rule by doing a roll up summary and saving that in a field and then validating that it should throw error when this summary field is greater than 100. It's not working.

Alternatively, I wrote a trigger and that too is not working. Please help.

trigger TrigAllocToTeam on Team_Member__c (before insert, before update) {
    for(Team_Member__c tm:Trigger.new){
        //Contact con=[SELECT Id FROM Contact where Id=:tm.Contact_Resource__r.id];
        List <Team_Member__c> tms = [SELECT ID, Allocation_to_Team__c FROM Team_Member__c 
                                    WHERE Contact_Resource__r.id =:tm.Contact_Resource__r.id];
        Decimal sum=0;
        for (Team_Member__c teamMem:tms){
            sum = sum + teamMem.Allocation_to_Team__c;
            if(sum>100){
                tm.addError('More than 100%');
            }
            
        }
    }
}
Prasanthi_s1505Prasanthi_s1505
Hi SambitNayak,

Pleasr try this below code to your requirement,
First write a trigger as below say "xyz.apxt" here I gave "ContactTrigger.apxt"

/***************************************************************/
trigger ContactTrigger on Team_Member__c(After Insert, After Update,After delete,After undelete) {
   ContactTriggerHelper cth =new ContactTriggerHelper();
    set<id> accIds = new set<id>();
    
    if(trigger.isAfter && (trigger.isInsert||trigger.isUpdate||trigger.isUndelete))
    {
        for(Team_Member__c tm : trigger.new)
        {
            if(tm.ContactId !=Null)
                accIds.add(tm.ContactId);
            cth.updateTotal(accIds);
            
        }            
    }
if(trigger.isAfter && (trigger.isUpdate||trigger.isDelete))
{
    for(Team_Member__c tm : trigger.old)
    {
            if(tm.ContactId !=Null)
                accIds.add(tm.ContactId);
        cth.updateTotal(accIds);
                
    }
}
}
/*********************************************************************/

Then create one helper class with below code say example "XyzHelper.apxc" here I gave "ContactTriggerHelper.apxc"

/******************************************************************/
public class ContactTriggerHelper {
    public void updateTotal(set<id> accids)       //Its Helper class method to perform Rollup summaries using aggregate SOQL 
    {
        List<AggregateResult> lstConRes = [SELECT ContactId,COUNT(id)cnt FROM Team_Member__c  WHERE ContactId in: accids GROUP BY ContactId];
        List<Contact> lstaccs = new List<Contact>();
        for(AggregateResult ar : lstConRes)
        {
            Contact con = new Contact();
            con.id = (Id)ar.get('ContactId');
            // Here in ur case the give the field name from the contact where to total has to be displayed (Ex:Total_Contacts__c I gave )
            con.Total_Contacts__c = (Decimal)ar.get('cnt');   
            lstAccs.add(con);
        } 
        
        Update lstAccs;
        
    }
}

Try this and if you found helpful please mark it as Best Answer.

Thank You,
Prasanthi
SambitNayakSambitNayak
Thanks so much @Prasanthi.

Why should it be an "After Trigger". I am restricting the user to enter values which amount to more that 100.
Example: I have a contact "Prasanthi". I need 3 allocations under this contact. I dont want the sum of all these 3 allocation amounts to exceed 100.

Hope you get the question. Thanks for your help though.
Prasanthi_s1505Prasanthi_s1505
Hi SambitNayak,

Yes, You dont need After trigger in ur case If u have to throw an Error if exceeds 100.
The above code works to find aggregate COUNT function.
   con.Total_Contacts__c = (Decimal)ar.get('cnt');    
After this statement give a IF condition..


Please update trigger according to your requirement..
I was more concentrated on the aggregate SOQL query so it makes simple.

If it was helpful mark it as Best Answer

Thanks,
Prasanthi