You need to sign in to do that
Don't have an account?
SambitNayak
Trigger to loop on related object
Hi,
I have a custom object "Team Member" which is related to Contact through lookup. The Team Member object has a field called "Allocation to team". My requirement is -- The sum of Allocations of the teams for a particular contact should not exceed 100.
I have the following code. But, it's breaking everytime the allocation sum reaches 100 (and it's not allowing me to change after that).
Please help.
-----------------My Code is below-------------------
public class TeamMemberTriggerHandler {
public static void checkTeamAllocation(List<Team_Member__c> TeamList, Map<Id, Team_Member__c> oldTeamList, boolean isInsert, boolean isUpdate){
Set<ID> listContacts = New Set<ID>();
for (Team_Member__c tm: TeamList){
listContacts.add(tm.Contact_Resource__c);
}
Map<Id,List<Team_Member__c>> mapContToTmMem = New Map<Id,List<Team_Member__c>>();
if(!listContacts.isEmpty()){
List<contact> conList = [SELECT Id, (SELECT Id, Allocation_to_Team__c FROM Team_Members__r) FROM contact WHERE Id IN :listContacts];
for(Contact con:conList){
mapContToTmMem.put(con.Id, con.Team_Members__r);
}
}
if(!mapContToTmMem.isEmpty()){
Decimal AllocationSum=0;
for(Team_Member__c tm:TeamList){
if(mapContToTmMem.containsKey(tm.Contact_Resource__c)){
for(Team_Member__c teamMem:mapContToTmMem.get(tm.Contact_Resource__c)){
AllocationSum =AllocationSum + teamMem.Allocation_to_Team__c;
if(AllocationSum > 100){
tm.addError('The total allocations for a particular ContactResource can\'t be more than 100%');
}
}
}
}
}
}
}
}
I have a custom object "Team Member" which is related to Contact through lookup. The Team Member object has a field called "Allocation to team". My requirement is -- The sum of Allocations of the teams for a particular contact should not exceed 100.
I have the following code. But, it's breaking everytime the allocation sum reaches 100 (and it's not allowing me to change after that).
Please help.
-----------------My Code is below-------------------
public class TeamMemberTriggerHandler {
public static void checkTeamAllocation(List<Team_Member__c> TeamList, Map<Id, Team_Member__c> oldTeamList, boolean isInsert, boolean isUpdate){
Set<ID> listContacts = New Set<ID>();
for (Team_Member__c tm: TeamList){
listContacts.add(tm.Contact_Resource__c);
}
Map<Id,List<Team_Member__c>> mapContToTmMem = New Map<Id,List<Team_Member__c>>();
if(!listContacts.isEmpty()){
List<contact> conList = [SELECT Id, (SELECT Id, Allocation_to_Team__c FROM Team_Members__r) FROM contact WHERE Id IN :listContacts];
for(Contact con:conList){
mapContToTmMem.put(con.Id, con.Team_Members__r);
}
}
if(!mapContToTmMem.isEmpty()){
Decimal AllocationSum=0;
for(Team_Member__c tm:TeamList){
if(mapContToTmMem.containsKey(tm.Contact_Resource__c)){
for(Team_Member__c teamMem:mapContToTmMem.get(tm.Contact_Resource__c)){
AllocationSum =AllocationSum + teamMem.Allocation_to_Team__c;
if(AllocationSum > 100){
tm.addError('The total allocations for a particular ContactResource can\'t be more than 100%');
}
}
}
}
}
}
}
}
So basically, I have gone throug your code and found that the custom field (Allocation_to_Team__c ), doesn't have any value while you are making the query. And for that reason, your code is not working. So for this reason, when you are adding the value to the AllocationSum value, the result i.e. AllocationSum is not increasing Hence it's not coming inside of this loop below:
if( AllocationSum > 100 ) {
tm.addError('The total allocations for a particular ContactResource can\'t be more than 100%');
}
Hope this helps, If you find your solution please mark it as the best answer.
Thanks
All Answers
Try Below Code Please Mark It As Best Answer If it Helps Thank You!
So basically, I have gone throug your code and found that the custom field (Allocation_to_Team__c ), doesn't have any value while you are making the query. And for that reason, your code is not working. So for this reason, when you are adding the value to the AllocationSum value, the result i.e. AllocationSum is not increasing Hence it's not coming inside of this loop below:
if( AllocationSum > 100 ) {
tm.addError('The total allocations for a particular ContactResource can\'t be more than 100%');
}
Hope this helps, If you find your solution please mark it as the best answer.
Thanks