• Zach Delacroix
  • NEWBIE
  • 55 Points
  • Member since 2015
  • System Administrator
  • Umbrella Corp

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 16
    Replies
Hi Experts,

I have created a trigger that updates three objects in one. (I know that will cause me Error just don't know how to fix it)

I heard about Batch Apex, but I have no Idea how to use this. Below is my code. I hope someone can help :)
 
public class ClsClientSignOff {

public void UpdateJobAllocation(List<Job_Allocation_Sign_Off__c> JASOs){
        
        set<id> jobid = new set<id>();
        set<id> resourceId = new set<id>();
        
        map<id,Job_Allocation_Sign_Off__c> jasomap = new map<id,Job_Allocation_Sign_Off__c>();
        
        for(Job_Allocation_Sign_Off__c jaso : [select id, name, Client_Sign_Off__r.job__c, Resource__c,StartTravel__c, FinishTravel__c,StartJob__c,FinishJob__c,Resource_Type__c,StartOdometer__c, Start_Break__c, End_Break__c
                                               from Job_Allocation_Sign_Off__c 
                                               where id in:JASOs]){
                                                   
            jobid.add(jaso.Client_Sign_Off__r.job__c);
            resourceId.add(jaso.Resource__c);
            jasomap.put(jaso.Id, jaso);
                                                   
        }
        
        list<sked__Job_Allocation__c> jalist = new list<sked__Job_Allocation__c>();
        list<sked__job__c> joblist = new list<sked__job__c>();
        list<sked__resource__c> resmap = new list<sked__resource__c>();

        for(sked__Job_Allocation__c ja : [select id, sked__Time_Start_Travel__c, sked__Resource__c,sked__Time_Checked_In__c,sked__Time_In_Progress__c,sked__Time_Completed__c,Resource_Type__c  
                                          from sked__Job_Allocation__c 
                                          where sked__Job__c in: jobid
                                          and sked__Resource__c in: resourceId]){
            
            for(id key : jasomap.keySet()){
                if(jasomap.get(key).Resource__c == ja.sked__Resource__c){
                    if(ja.Resource_Type__c == 'Person'){
                        ja.sked__Time_Start_Travel__c = jasomap.get(key).StartTravel__c;
                        ja.sked__Time_Checked_In__c = jasomap.get(key).FinishTravel__c;
                        ja.sked__Time_In_Progress__c = jasomap.get(key).StartJob__c;
                        ja.sked__Time_Completed__c = jasomap.get(key).FinishJob__c;
                    }
                    if(ja.Resource_Type__c == 'Asset'){
                        for(sked__job__c job : [select id, name, sked__Start__c, sked__Finish__c, sked__Duration__c from sked__job__c where id in: jobid]){
                            job.sked__Start__c = jasomap.get(key).StartJob__c;
                            job.sked__Finish__c = jasomap.get(key).FinishJob__c;
                            //Job Duration
                            if(jasomap.get(key).StartJob__c != null && jasomap.get(key).FinishJob__c != null ){
                                job.sked__Duration__c = (jasomap.get(key).FinishJob__c.getTime() - jasomap.get(key).StartJob__c.getTime()) / 60000;
                            }
                            
                            joblist.add(job);
                        }
                        for(sked__resource__c Resource : [select LatestOdometer__c from sked__resource__c where id = : ja.sked__Resource__c]){
                            resource.LatestOdometer__c = jasomap.get(key).StartOdometer__c;
                            resmap.add(resource);
                        }
                    }
                    jalist.add(ja);
                }
            }
        }
        if(jalist != null) { update jalist; }
        if(joblist != null) {update joblist;}
        if(resmap != null) {update resmap;}
        
    }

}

I don't know which part of my code I should change for this. Any help?

Thanks,

-Zach

 

Hi Experts,

I have a simple question and I'm not sure why it's not returning correct data..

In my Class, I want to get the Parent Id of the Object. I'll need to traverse from Child, then Parent, and Parent of the Parent. Goes like this.

myObject > myObjectParent >MainParent.

myObject is where my trigger is, I want to be able to get the MainParent's ID and Here's how I do it. (The usual approach when I get Ids)

public class testClass {

    public void myMethod(List<myObject__c> myObjects){
       Set<Id> myObjectID = new Set<Id>();
    
       for(myObject__c myObject : MyObjects){
           myObjectID.add(myObject.MyParentObject__r.MainParentID);
       }
    }
}
Am I doing it wrong for this kind of situation? The code above returns Null so I can't use it in my query..

Can anyone please advise how to get the MainParent's ID?

Thank you!

-Zach

 
Hi Guys!

I have a question about Populating value of the Map. While doing some test today, I noticed that Map returns different value.

Here's an example:

Cobe below gives me just 1 value. But When I look on the Account Related list, there are more than 5 records that meets my query.
Map<String,CustomOBJ__c> ObjMap = new Map<String,CustomOBJ__c>();
        
        For(CustomOBJ__c Obj: [SELECT Id, Name, Account__c, Field1, Field2
                                   	  FROM CustomOBJ__c 
                                   	  WHERE Account__c IN: AccountId]){  //<<This is my Set Account ID
            ObjMap.put(Obj.Account__c, Obj);
        }
        
        for(String R: ObjMap.keySet()){
            System.debug(ObjMap.get(R).Name);
        }

This one below shows correct Value. Shows all five records under the Account Related List. I simply changed ObjMap.put(Obj.Account__c+Field1, Obj); when populating the Map with the customOBJ records.
 
Map<String,CustomOBJ__c> ObjMap = new Map<String,CustomOBJ__c>();
        
        For(CustomOBJ__c Obj: [SELECT Id, Name, Account__c, Field1, Field2
                                   	  FROM CustomOBJ__c 
                                   	  WHERE Account__c IN: AccountId]){  //<<This is my Set Account ID
            ObjMap.put(Obj.Account__c+Field1, Obj);
        }
        
        for(String O: ObjMap.keySet()){
            System.debug(ObjMap.get(O).Name);
        }

My expected Result it that is I have 5 records under Account, and if I do query like the one above, it will pull up all 5 records.

but I only need ObjMap.put(Obj.Account__c, obj); NOT ObjMap.put(Obj.Account__c+Field1, Obj);

This can be helpful on my learning with apex code. Can anyone help with explantion on this please.. Thanks!

-Zach


 
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
 
Hey Guys!

I'd like some help about Trigger Handler. I want to start with the basics and I'd like to have assistance with step by step explanation on how this really works. (Based on your expert experience)

I've read the link below which was provided to me in my previous question but it is difficult for me to understand it for now. (Still new to apex)
https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices

For example, I've created this one below as a start. The code simply create record when I call it in my Trigger but I don't know if it's correct.

CLASS
 
public with sharing class ClsTriggerHandlerTest {

    public static void createTestRecords(List<TestObj__c> testObjs){

        List<TestObj__c> SaveRecord = new List<TestObj__c>();
        
        for(TestObj__c testObj : testObjs){
            
            testObj.Name = 'Created from ClsTriggerHandler';
            SaveRecord.add(testObj );
        }

        insert SaveRecord;
    }
}

the above Class doesn't seem to return an Error so I assume it's correct (But I'm sure there's something else I need to add)

Here's how I try to use the class in my trigger but it is returning the error:
"Method does not exist or incorrect signature: ClsTriggerHandler.theResult(TestObj__c)"

TRIGGER
trigger TriggerTest on TestObj__c (before insert, before update) {

        for(TestObj__c TestObj: trigger.new){
              ClsTriggerHandler.theResult(TestObj);
    }
}


I'm a visual learner so please provide me the very basic and very simple explanation :)

Thanks a lot as always :)

-Zach
 
Hi Experts!

I'm a newbie and I've created a Trigger below with the help of awesome people here. This works fine but my friend suggested to study about the Trigger Handler so we can limit triggers in object and my code will be easier to debug. I want to learn the Best Practices on this so please help and advise how to put this with Class. 
 
trigger UpdateHourlyRate on Job__c (Before insert, Before update) {

    Set<Id> AccID = new Set<Id>();
    Set<String> JobType = new Set<String>();
    
    Map<String,Rate_card__c> RateCardMap = new Map<String,Rate_card__c>();
    
    for (Job__c Job: Trigger.new){AccID.add(Job.Account__c); JobType.add(Job.Type__c);}
    
    For(Rate_Card__c RateCard : [SELECT Id, Hourly_Rate__c, Account__c, Type__c 
                                 FROM Rate_card__c 
                                 WHERE Account__c IN: AccID]){
								 	
                                     RateCardMap.put(RateCard.Account__c + RateCard.Type__c, RateCard);
  
                                 }
 
	For (Job__c Jobs: Trigger.new ){
            
            if(RateCardMap.containsKey(Jobs.Account__c+Jobs.Type__c)){ 
    
                Jobs.Hourly_Rate__c = RateCardMap.get(Jobs.Account__c + Jobs.Type__c).Hourly_Rate__c;

    }
}

I'm not sure how to start though. basically, I thought of building a class like below.
 
public class JobHourlyRateUpdate {

    Set<Id> AccID = new Set<Id>();
    
    public Static void computeHourlyRate(List<Job__c> Jobs){
        
            For(Job__c Job:Jobs){
                AccID.add(Job.Account__c);
            }
            
            Map<String,Rate_card__c> RateCardMap = new Map<String,Rate_card__c>();
            
            For(Rate_Card__c RateCard : [SELECT Id, Hourly_Rate__c, Account__c, Type__c 
                                     FROM Rate_card__c 
                                     WHERE Account__c IN: AccID]){
                                        
                                         RateCardMap.put(RateCard.Account__c + RateCard.Type__c, RateCard);
      
                                     }
        
    }
}


Here's what my code does:

When Job Record is Created or Updated, find Rate Card from Account (Related List) and update the Job's Hourly Rate if the Ratecard has the same Type as the Job.

Result Should be something like below.

User-added image


It would be fantastic if there are documents you can reffer me so I can start learning about this :)

Thanks in Advance!
Hi Guys!

Newbie here and I need someone who can help me bulkify my code. I've been struggling with it so I decided to post it :)

Below is My scenario:

User-added image

Job Record is associated to Account, the Account also have related list called Rate Cards.
As you can see, both Objects are tied to account and both have fields Type and Hourly Rate.

My Task is to Copy the Hourly Rate of the Rate Card in the Associated account that has the same type as the Job. (See Image Above)

I was able to do it with the Code below.
 
trigger UpdateHourlyRate on Job__c (Before insert, Before update) {
    
    Set<Id> AccID = new Set<Id>();

    Map<String,Rate_card__c> RateCardMap = new Map<String,Rate_card__c>();
    
    for (Job__c Job: Trigger.new){AccID.add(Job.Account__c);}
    
    For(Rate_Card__c RateCard : [SELECT Id, Hourly_Rate__c, Account__c, Type__c 
                                 FROM Rate_card__c 
                                 WHERE Account__c IN: AccID]){

                                     RateCardMap.put(RateCard.Account__c + RateCard.Type__c, RateCard);
                                 }
    
    For (Job__c Jobs: Trigger.new ){

        Jobs.Hourly_Rate__c = RateCardMap.get(Jobs.Account__c + Jobs.Type__c).Hourly_Rate__c;
    }
}

However, I know that this one is not bulkified and I was getting a lots of Error when I try bulkify it. I tried adding list and Updating the List (Like some of the videos I've watched and Tests I created) but no Luck.

Can someone please guide me how to bulkify my code? what are the best practice for this? :)

Thanks in advance!

\m/
Hi Experts,

I have created a trigger that updates three objects in one. (I know that will cause me Error just don't know how to fix it)

I heard about Batch Apex, but I have no Idea how to use this. Below is my code. I hope someone can help :)
 
public class ClsClientSignOff {

public void UpdateJobAllocation(List<Job_Allocation_Sign_Off__c> JASOs){
        
        set<id> jobid = new set<id>();
        set<id> resourceId = new set<id>();
        
        map<id,Job_Allocation_Sign_Off__c> jasomap = new map<id,Job_Allocation_Sign_Off__c>();
        
        for(Job_Allocation_Sign_Off__c jaso : [select id, name, Client_Sign_Off__r.job__c, Resource__c,StartTravel__c, FinishTravel__c,StartJob__c,FinishJob__c,Resource_Type__c,StartOdometer__c, Start_Break__c, End_Break__c
                                               from Job_Allocation_Sign_Off__c 
                                               where id in:JASOs]){
                                                   
            jobid.add(jaso.Client_Sign_Off__r.job__c);
            resourceId.add(jaso.Resource__c);
            jasomap.put(jaso.Id, jaso);
                                                   
        }
        
        list<sked__Job_Allocation__c> jalist = new list<sked__Job_Allocation__c>();
        list<sked__job__c> joblist = new list<sked__job__c>();
        list<sked__resource__c> resmap = new list<sked__resource__c>();

        for(sked__Job_Allocation__c ja : [select id, sked__Time_Start_Travel__c, sked__Resource__c,sked__Time_Checked_In__c,sked__Time_In_Progress__c,sked__Time_Completed__c,Resource_Type__c  
                                          from sked__Job_Allocation__c 
                                          where sked__Job__c in: jobid
                                          and sked__Resource__c in: resourceId]){
            
            for(id key : jasomap.keySet()){
                if(jasomap.get(key).Resource__c == ja.sked__Resource__c){
                    if(ja.Resource_Type__c == 'Person'){
                        ja.sked__Time_Start_Travel__c = jasomap.get(key).StartTravel__c;
                        ja.sked__Time_Checked_In__c = jasomap.get(key).FinishTravel__c;
                        ja.sked__Time_In_Progress__c = jasomap.get(key).StartJob__c;
                        ja.sked__Time_Completed__c = jasomap.get(key).FinishJob__c;
                    }
                    if(ja.Resource_Type__c == 'Asset'){
                        for(sked__job__c job : [select id, name, sked__Start__c, sked__Finish__c, sked__Duration__c from sked__job__c where id in: jobid]){
                            job.sked__Start__c = jasomap.get(key).StartJob__c;
                            job.sked__Finish__c = jasomap.get(key).FinishJob__c;
                            //Job Duration
                            if(jasomap.get(key).StartJob__c != null && jasomap.get(key).FinishJob__c != null ){
                                job.sked__Duration__c = (jasomap.get(key).FinishJob__c.getTime() - jasomap.get(key).StartJob__c.getTime()) / 60000;
                            }
                            
                            joblist.add(job);
                        }
                        for(sked__resource__c Resource : [select LatestOdometer__c from sked__resource__c where id = : ja.sked__Resource__c]){
                            resource.LatestOdometer__c = jasomap.get(key).StartOdometer__c;
                            resmap.add(resource);
                        }
                    }
                    jalist.add(ja);
                }
            }
        }
        if(jalist != null) { update jalist; }
        if(joblist != null) {update joblist;}
        if(resmap != null) {update resmap;}
        
    }

}

I don't know which part of my code I should change for this. Any help?

Thanks,

-Zach

 

Hi Experts,

I have a simple question and I'm not sure why it's not returning correct data..

In my Class, I want to get the Parent Id of the Object. I'll need to traverse from Child, then Parent, and Parent of the Parent. Goes like this.

myObject > myObjectParent >MainParent.

myObject is where my trigger is, I want to be able to get the MainParent's ID and Here's how I do it. (The usual approach when I get Ids)

public class testClass {

    public void myMethod(List<myObject__c> myObjects){
       Set<Id> myObjectID = new Set<Id>();
    
       for(myObject__c myObject : MyObjects){
           myObjectID.add(myObject.MyParentObject__r.MainParentID);
       }
    }
}
Am I doing it wrong for this kind of situation? The code above returns Null so I can't use it in my query..

Can anyone please advise how to get the MainParent's ID?

Thank you!

-Zach

 
Hi Guys!

I have a question about Populating value of the Map. While doing some test today, I noticed that Map returns different value.

Here's an example:

Cobe below gives me just 1 value. But When I look on the Account Related list, there are more than 5 records that meets my query.
Map<String,CustomOBJ__c> ObjMap = new Map<String,CustomOBJ__c>();
        
        For(CustomOBJ__c Obj: [SELECT Id, Name, Account__c, Field1, Field2
                                   	  FROM CustomOBJ__c 
                                   	  WHERE Account__c IN: AccountId]){  //<<This is my Set Account ID
            ObjMap.put(Obj.Account__c, Obj);
        }
        
        for(String R: ObjMap.keySet()){
            System.debug(ObjMap.get(R).Name);
        }

This one below shows correct Value. Shows all five records under the Account Related List. I simply changed ObjMap.put(Obj.Account__c+Field1, Obj); when populating the Map with the customOBJ records.
 
Map<String,CustomOBJ__c> ObjMap = new Map<String,CustomOBJ__c>();
        
        For(CustomOBJ__c Obj: [SELECT Id, Name, Account__c, Field1, Field2
                                   	  FROM CustomOBJ__c 
                                   	  WHERE Account__c IN: AccountId]){  //<<This is my Set Account ID
            ObjMap.put(Obj.Account__c+Field1, Obj);
        }
        
        for(String O: ObjMap.keySet()){
            System.debug(ObjMap.get(O).Name);
        }

My expected Result it that is I have 5 records under Account, and if I do query like the one above, it will pull up all 5 records.

but I only need ObjMap.put(Obj.Account__c, obj); NOT ObjMap.put(Obj.Account__c+Field1, Obj);

This can be helpful on my learning with apex code. Can anyone help with explantion on this please.. Thanks!

-Zach


 
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
 
Hey Guys!

I'd like some help about Trigger Handler. I want to start with the basics and I'd like to have assistance with step by step explanation on how this really works. (Based on your expert experience)

I've read the link below which was provided to me in my previous question but it is difficult for me to understand it for now. (Still new to apex)
https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices

For example, I've created this one below as a start. The code simply create record when I call it in my Trigger but I don't know if it's correct.

CLASS
 
public with sharing class ClsTriggerHandlerTest {

    public static void createTestRecords(List<TestObj__c> testObjs){

        List<TestObj__c> SaveRecord = new List<TestObj__c>();
        
        for(TestObj__c testObj : testObjs){
            
            testObj.Name = 'Created from ClsTriggerHandler';
            SaveRecord.add(testObj );
        }

        insert SaveRecord;
    }
}

the above Class doesn't seem to return an Error so I assume it's correct (But I'm sure there's something else I need to add)

Here's how I try to use the class in my trigger but it is returning the error:
"Method does not exist or incorrect signature: ClsTriggerHandler.theResult(TestObj__c)"

TRIGGER
trigger TriggerTest on TestObj__c (before insert, before update) {

        for(TestObj__c TestObj: trigger.new){
              ClsTriggerHandler.theResult(TestObj);
    }
}


I'm a visual learner so please provide me the very basic and very simple explanation :)

Thanks a lot as always :)

-Zach
 
Hi Experts!

I'm a newbie and I've created a Trigger below with the help of awesome people here. This works fine but my friend suggested to study about the Trigger Handler so we can limit triggers in object and my code will be easier to debug. I want to learn the Best Practices on this so please help and advise how to put this with Class. 
 
trigger UpdateHourlyRate on Job__c (Before insert, Before update) {

    Set<Id> AccID = new Set<Id>();
    Set<String> JobType = new Set<String>();
    
    Map<String,Rate_card__c> RateCardMap = new Map<String,Rate_card__c>();
    
    for (Job__c Job: Trigger.new){AccID.add(Job.Account__c); JobType.add(Job.Type__c);}
    
    For(Rate_Card__c RateCard : [SELECT Id, Hourly_Rate__c, Account__c, Type__c 
                                 FROM Rate_card__c 
                                 WHERE Account__c IN: AccID]){
								 	
                                     RateCardMap.put(RateCard.Account__c + RateCard.Type__c, RateCard);
  
                                 }
 
	For (Job__c Jobs: Trigger.new ){
            
            if(RateCardMap.containsKey(Jobs.Account__c+Jobs.Type__c)){ 
    
                Jobs.Hourly_Rate__c = RateCardMap.get(Jobs.Account__c + Jobs.Type__c).Hourly_Rate__c;

    }
}

I'm not sure how to start though. basically, I thought of building a class like below.
 
public class JobHourlyRateUpdate {

    Set<Id> AccID = new Set<Id>();
    
    public Static void computeHourlyRate(List<Job__c> Jobs){
        
            For(Job__c Job:Jobs){
                AccID.add(Job.Account__c);
            }
            
            Map<String,Rate_card__c> RateCardMap = new Map<String,Rate_card__c>();
            
            For(Rate_Card__c RateCard : [SELECT Id, Hourly_Rate__c, Account__c, Type__c 
                                     FROM Rate_card__c 
                                     WHERE Account__c IN: AccID]){
                                        
                                         RateCardMap.put(RateCard.Account__c + RateCard.Type__c, RateCard);
      
                                     }
        
    }
}


Here's what my code does:

When Job Record is Created or Updated, find Rate Card from Account (Related List) and update the Job's Hourly Rate if the Ratecard has the same Type as the Job.

Result Should be something like below.

User-added image


It would be fantastic if there are documents you can reffer me so I can start learning about this :)

Thanks in Advance!
Hi Guys!

Newbie here and I need someone who can help me bulkify my code. I've been struggling with it so I decided to post it :)

Below is My scenario:

User-added image

Job Record is associated to Account, the Account also have related list called Rate Cards.
As you can see, both Objects are tied to account and both have fields Type and Hourly Rate.

My Task is to Copy the Hourly Rate of the Rate Card in the Associated account that has the same type as the Job. (See Image Above)

I was able to do it with the Code below.
 
trigger UpdateHourlyRate on Job__c (Before insert, Before update) {
    
    Set<Id> AccID = new Set<Id>();

    Map<String,Rate_card__c> RateCardMap = new Map<String,Rate_card__c>();
    
    for (Job__c Job: Trigger.new){AccID.add(Job.Account__c);}
    
    For(Rate_Card__c RateCard : [SELECT Id, Hourly_Rate__c, Account__c, Type__c 
                                 FROM Rate_card__c 
                                 WHERE Account__c IN: AccID]){

                                     RateCardMap.put(RateCard.Account__c + RateCard.Type__c, RateCard);
                                 }
    
    For (Job__c Jobs: Trigger.new ){

        Jobs.Hourly_Rate__c = RateCardMap.get(Jobs.Account__c + Jobs.Type__c).Hourly_Rate__c;
    }
}

However, I know that this one is not bulkified and I was getting a lots of Error when I try bulkify it. I tried adding list and Updating the List (Like some of the videos I've watched and Tests I created) but no Luck.

Can someone please guide me how to bulkify my code? what are the best practice for this? :)

Thanks in advance!

\m/