+ Start a Discussion
SambitNayakSambitNayak 

trigger to avoid the sum of child objects to exceed 100

Hi,
I have Contact object with Team Member as child object. The sum of all the allocations of Team Members under a certain contact cant exceed 100.  
This helper class is not working properly. Please advise:

 

public class TrigAllocToTeamHandler {
    public static void chkTeamAlloc(List<Team_Member__c> teamList){
        
        for(Team_Member__c tm:teamList){
        List <Team_Member__c> tms = [SELECT ID, Allocation_to_Team__c FROM Team_Member__c 
                                     WHERE Contact_Resource__c =:tm.Contact_Resource__c];  
                                       
            Integer sum=0;
         
        for (Team_Member__c teamMem:tms){
            sum += Integer.valueOf(teamMem.Allocation_to_Team__c);
            if(sum < 100){
                tms1.add(teamMem);
            }
            else{
                tm.addError('Total allocation to all the Teams cant be more than 100%');
                break;
            }            
        }
    }
    }

}

Suraj Tripathi 47Suraj Tripathi 47
Hi Sambit,

Please use below code, I hope it will work
 
public class TrigAllocToTeamHandler {
    public static void chkTeamAlloc(List<Team_Member__c> teamList){
	
	set<id> conId = new set<id>();
	
	for(Team_Member__c tmObj:teamList){
	if(tmObj.Contact_Resource__c != null){
	        conId.add(tmObj.Contact_Resource__c);
	}
	}
	
        list<Contact> conList = [select id,(select id from Team_Members__c) from Contact];
		
		map<id,list<Team_Members__c>> conIdToTeamMembersList = new map<id,list<Team_Members__c>>();

        for(Contact conObj:conList){
		         conIdToTeamMembersList.put(conObj,Team_Members__c);
		} 		
		
        for(Team_Member__c tm:teamList){
		
		if(conIdToTeamMembersList.ContainsKey(tm.Contact_Resource__c) && conIdToTeamMembersList.get(tm.Contact_Resource__c).size()<100){
       		    
            list<Contact> conList =   conIdToTeamMembersList.get(tm.Contact_Resource__c);
            conList.add(tm); 			
		
		}
		
		else{
		tm.addError('Total allocation to all the Teams cant be more than 100%');
		}
        
    }
    }
               

}

If you find your Solution then mark this as the best answer.

Thank you!
Regards,
Suraj Tripathi  




 
AbhinavAbhinav (Salesforce Developers) 
Not able to understand yoir use case However
Few issues in general with your code ;
> You are use query inside for loop, avoid it .Use Map. 

> tms1.add(teamMem); ---> you are using tms1  is it a list . I can't see where you have declared that.

> why you are using break?

Thanks!
CharuDuttCharuDutt
Hii Sambit
Try Below Trigger
trigger NumberOfChild on Team_Member__c (After Insert,After Update,After Delete) {
   List<Contact> conList=new List<Contact>();

    Set<Id> setConnIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Team_Member__c tm: Trigger.new){
            if(tm.Contact_Resource__c != null){
            setConnIds .add(tm.Contact_Resource__c );
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Team_Member__c tm: Trigger.new){ 
            if(tm.Contact_Resource__c !=Trigger.oldMap.get(tm.Id).Contact_Resource__c ){
               	setConnIds .add(tm.Contact_Resource__c );
                setConnIds .add(Trigger.oldMap.get(tm.Id).Contact_Resource__c );
            	}
          
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Team_Member__c tm: Trigger.old) { 
            if(tm.Contact_Resource__c != null){
            setConnIds .add(tm.Contact_Resource__c );
            	}
        	}
        }
    }    
    for(Contact Con:[Select id,Total_Team_Members__c ,(Select id from Team_Members__r ) from Contact where Id in : setConnIds ]){
     if(Con.Team_Members__r .size() < 100){
        Con.Total_Team_Members__c = Con.Team_Members__r .size();
       }
        conList.add(Con);

    }
    if(conList.size()>0){
        update conList;     
    }
Please Mark It As Best Answer If It Helps
Thank You!
SambitNayakSambitNayak

Hi @Suraj,
I find your solution most suitable for my query. But, still I have some questions.

Qestion#1:
For this line:     conIdToTeamMembersList.put(conObj,Team_Member__c);
I'm getting the error: "Variable does not exist: Team_Member__c"

2nd question:  I didnt understand this line of code:

"if(conIdToTeamMembersList.ContainsKey(tm.Contact_Resource__c) && conIdToTeamMembersList.get(tm.Contact_Resource__c).size()<100){"

I have some team members under a contact whose sum of allocations shoun't exceed 100.
i.e: If Contact 'A' has 3 team members and they have 30%, 30% and 40% allocations respectively. This is a positive scenario.
The sum of these percentages can never exceed 100%.

Please let me know if that makes sense.

Thanks for your help.