You need to sign in to do that
Don't have an account?
Zach 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.
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.
Thank you so much in advance!
-Zach
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.
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
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 -
*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.
I have modified the code a bit, check it out.
Thanks
AR
If you find the reply useful that solves your problem then please mark it as best answer.
However it's still doing it. see image below.
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
I have made a small change that should fix the issue. Please test it. Thanks
AR
If you find the reply useful that solves your problem then please mark it as best answer.
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