You need to sign in to do that
Don't have an account?

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;
}
}
}
}
}
Please use below code, I hope it will work
If you find your Solution then mark this as the best answer.
Thank you!
Regards,
Suraj Tripathi
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!
Try Below Trigger Please Mark It As Best Answer If It Helps
Thank You!
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.