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 

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%');
                    }
                        } 

                    }
                } 

            }
        }
    }
    
}
 
Best Answer chosen by SambitNayak
Suraj Tripathi 47Suraj Tripathi 47
Hi Sambit,
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

CharuDuttCharuDutt
Hii Sambit 
Try Below Code
public class TeamMemberTriggerHandler {
    public static Void InsertMethod(List<Team_Member__c> TeamList,boolean isInsert ){
        Set<Id> listContacts = New Set<Id>();
        Integer LocationSum;
        for (Team_Member__c tm: TeamList){
            if(tm.Contact_Resource__c != null){
                listContacts.add(tm.Contact_Resource__c);
            }
        }
        List<contact> conList = [SELECT Id, (SELECT Id, Allocation_to_Team__c FROM Team_Members__r) FROM contact WHERE Id IN :listContacts];
        for(contact Conlist :conList){
            for(Team_Member__c tm:Conlist.Team_Members__r){ 
                LocationSum =+ tm.Allocation_to_Team__c;
                if(LocationSum > 100){
                    tm.addError('The total allocations for a particular ContactResource can\'t be more than 100%');
                }
            }
        } 
    }
    public static Void UpdateMethod(List<Team_Member__c> TeamList, Map<Id, Team_Member__c> oldTeamList,boolean isUpdate){
        Set<Id> listContacts = New Set<Id>();
        Integer LocationSum;
        for (Team_Member__c tm: TeamList){
            if(tm.Contact_Resource__c != null && tm.Contact_Resource__c != oldTeamList.get(tm.Id).Contact_Resource__c){
                listContacts.add(tm.Contact_Resource__c);
            }
        }
        List<contact> conList = [SELECT Id, (SELECT Id, Allocation_to_Team__c FROM Team_Members__r) FROM contact WHERE Id IN :listContacts];
        for(contact Conlist :conList){
            for(Team_Member__c tm:Conlist.Team_Members__r){ 
                LocationSum =+ tm.Allocation_to_Team__c;
                if(LocationSum > 100){
                    tm.addError('The total allocations for a particular ContactResource can\'t be more than 100%');
                }
            }
        } 
    }
}
Please Mark It As Best Answer If it Helps Thank You!
Suraj Tripathi 47Suraj Tripathi 47
Hi Sambit,
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
This was selected as the best answer