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
Zach DelacroixZach Delacroix 

Help with Trigger Logic

Hi guys!

I'm working on a trigger and stuck on one logic and I'm wondering if someone here can help. I'm a newbie and still exploring things here. What my trigger does is that it updates the Job Record's Hourly Rate with the Rate Card's Hourly rate.

So basically, if the Job matches the Rate Card on the associated Job Account based on the Type field, then get the hourly rate of Rate card and update the Job's Hourly Rate if the Rate Card that matches is Active. Please See image below.

User-added image

My Trigger is working correctly but here's another logic I want to add.

Notice that Account can have multiple rate cards with the Same Type.. Example is RC-0000 and RC-0013 has the same type which is 'A' but different Hourly Rate.. What I would like to do is that, if there are rate cards in the Job's associated account that has same type, then I'd like the value to be Null.

Can anyone please give me an Idea?

Here's my code to start with.
 
public with sharing class JobTriggerHandler {

    Public void UpdateHourlyRate(List<Job__c> Jobs){
        
        //Hold Account ID
        Set<Id> accID = new Set<Id>();
        
        //Populate accID with the Jobs Account ID to be used in Query
        For(Job__c Job: Jobs){accID.add(Job.Account__c);}
        
        Map<String,Rate_Card__c> RateCardMap = new Map<String,Rate_Card__c>();

        //Populate RateCardMap with Rate cards where the account is equal to Job's Account and is Active
        For(Rate_Card__c RateCards : [SELECT Id, Name, Hourly_Rate__c, Active__c, Type__c, Account__c
                                      FROM Rate_Card__c
                                      WHERE Account__c IN: AccID
                                      AND Active__c = True]){
                                          
                                          RateCardMap.put(RateCards.Account__c+RateCards.Type__c, RateCards);
                                      }
        
		//Update Hourly Rate field
        For(Job__c Job : Jobs){
            //Checks if there's a match in the query and update with the value. 
            if(RateCardMap.containsKey(Job.Account__c+Job.Type__c)){
                Job.Hourly_Rate__c = RateCardMap.get(Job.Account__c+Job.Type__c).Hourly_Rate__c;
            } Else {Job.Hourly_Rate__c = Null;}
        }
    }
}


Thank you so much in advance!


-Zach
 
Anupam RastogiAnupam Rastogi
Hi Zach,

While you are creating the RateCardMap, before putting a record for a Rate Card check if the combination of Rate Card Account and the Type already exists or not using containsKey(). If it does then replace that value with null. Something like - 
 
public with sharing class JobTriggerHandler {

    Public void UpdateHourlyRate(List<Job__c> Jobs){
        
        //Hold Account ID
        Set<Id> accID = new Set<Id>();
        
        //Populate accID with the Jobs Account ID to be used in Query
        For(Job__c Job: Jobs){accID.add(Job.Account__c);}
        
        Map<String,Rate_Card__c> RateCardMap = new Map<String,Rate_Card__c>();

        //Populate RateCardMap with Rate cards where the account is equal to Job's Account and is Active
        For(Rate_Card__c RateCards : [SELECT Id, Name, Hourly_Rate__c, Active__c, Type__c, Account__c
                                      FROM Rate_Card__c
                                      WHERE Account__c IN: AccID
                                      AND Active__c = True]){
                                          if(RateCardMap.containsKey(RateCards.Account__c+RateCards.Type__c)) {
                                                 String temp = RateCardMap.remove(RateCards.Account__c+RateCards.Type__c);
                                                 RateCardMap.put(null, RateCards);
                                          }
                                          else
                                                 RateCardMap.put(RateCards.Account__c+RateCards.Type__c, RateCards);
                                      }
        
		//Update Hourly Rate field
        For(Job__c Job : Jobs){
            //Checks if there's a match in the query and update with the value. 
            if(RateCardMap.containsKey(Job.Account__c+Job.Type__c)){
                Job.Hourly_Rate__c = RateCardMap.get(Job.Account__c+Job.Type__c).Hourly_Rate__c;
            } Else {Job.Hourly_Rate__c = Null;}
        }
    }
}
*Just check the syntax etc before running it.

Thanks
AR

If you find the reply useful that solves your problem then please mark it as best answer.
Zach DelacroixZach Delacroix
Thanks a lot AR! Your approach is working when there's just two Rate Cards with the Same Type.. However, when I go create the Third Rate Cards (Meaning I have more than 2 Rate cards with the Same name, it still gets one of the value) What i want is that if there are more than 1 Rate cards with the Same Type, then give me Null value. How do we do that? Like RateCardMap.Size() == 1? Thanks!
Anupam RastogiAnupam Rastogi
Hi Zach,

I have modified the code a bit, check it out.
public with sharing class JobTriggerHandler {

    Public void UpdateHourlyRate(List<Job__c> Jobs){
        
        //Hold Account ID
        Set<Id> accID = new Set<Id>();
        
        //Populate accID with the Jobs Account ID to be used in Query
        For(Job__c Job: Jobs){accID.add(Job.Account__c);}
        
        Map<String,Rate_Card__c> RateCardMap = new Map<String,Rate_Card__c>();

        //Populate RateCardMap with Rate cards where the account is equal to Job's Account and is Active
        For(Rate_Card__c RateCards : [SELECT Id, Name, Hourly_Rate__c, Active__c, Type__c, Account__c
                                      FROM Rate_Card__c
                                      WHERE Account__c IN: AccID
                                      AND Active__c = True]){
                                          if(RateCardMap.containsKey(RateCards.Account__c+RateCards.Type__c) && RateCardMap.get(RateCards.Account__c+RateCards.Type__c) != null) {
                                                 String temp = RateCardMap.remove(RateCards.Account__c+RateCards.Type__c);
                                                 RateCardMap.put(RateCards.Account__c+RateCards.Type__c, null);
                                          }
                                          else
                                                 RateCardMap.put(RateCards.Account__c+RateCards.Type__c, RateCards);
                                      }
        
		//Update Hourly Rate field
        For(Job__c Job : Jobs){
            //Checks if there's a match in the query and update with the value. 
            if(RateCardMap.containsKey(Job.Account__c+Job.Type__c) && RateCardMap.get(Job.Account__c+Job.Type__c) != null){
                Job.Hourly_Rate__c = RateCardMap.get(Job.Account__c+Job.Type__c).Hourly_Rate__c;
            } Else {Job.Hourly_Rate__c = null;}
        }
    }
}

Thanks
AR

If you find the reply useful that solves your problem then please mark it as best answer.
 
Zach DelacroixZach Delacroix
Thanks AR,

However it's still doing it. see image below.

User-added image

Those 99 value on the Report should be blank because there are more than 1 A type on the Rate Card.

What i'm thinking is that is should look on the Size of the Map or something like that.. if it's equal to 1 then do the update, if not then make it null. But I don't know how to put something like that in the logic.

Thanks for the help :)

-Zach
Anupam RastogiAnupam Rastogi
Hi Zach,

I have made a small change that should fix the issue. Please test it.
public with sharing class JobTriggerHandler {

    Public void UpdateHourlyRate(List<Job__c> Jobs){
        
        //Hold Account ID
        Set<Id> accID = new Set<Id>();
        
        //Populate accID with the Jobs Account ID to be used in Query
        For(Job__c Job: Jobs){accID.add(Job.Account__c);}
        
        Map<String,Rate_Card__c> RateCardMap = new Map<String,Rate_Card__c>();

        //Populate RateCardMap with Rate cards where the account is equal to Job's Account and is Active
        For(Rate_Card__c RateCards : [SELECT Id, Name, Hourly_Rate__c, Active__c, Type__c, Account__c
                                      FROM Rate_Card__c
                                      WHERE Account__c IN: AccID
                                      AND Active__c = True]){
                                          if(RateCardMap.containsKey(RateCards.Account__c+RateCards.Type__c)) {
                                              if(RateCardMap.get(RateCards.Account__c+RateCards.Type__c) != null)
                                                 String temp = RateCardMap.remove(RateCards.Account__c+RateCards.Type__c);
                                                 RateCardMap.put(RateCards.Account__c+RateCards.Type__c, null);}
                                          }
                                          else
                                                 RateCardMap.put(RateCards.Account__c+RateCards.Type__c, RateCards);
                                      }
        
		//Update Hourly Rate field
        For(Job__c Job : Jobs){
            //Checks if there's a match in the query and update with the value. 
            if(RateCardMap.containsKey(Job.Account__c+Job.Type__c) && RateCardMap.get(Job.Account__c+Job.Type__c) != null){
                Job.Hourly_Rate__c = RateCardMap.get(Job.Account__c+Job.Type__c).Hourly_Rate__c;
            } Else {Job.Hourly_Rate__c = null;}
        }
    }
}
Thanks
AR

If you find the reply useful that solves your problem then please mark it as best answer.
 
Zach DelacroixZach Delacroix
Thank you AR. But it still not working that way. I will try to figure this out my self. Have a great day! -Zach
Anupam RastogiAnupam Rastogi
Hi Zach,

I was wondering what could be the issue here, because I ran this code at my end with similar objects and it worked for me.

Thanks
AR