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
Austin_SteveAustin_Steve 

Trigger question

I'm Trying to write a trigger that I can call from multiple custom objects.  I would like the trigger to pass an id from the object which is currently in focus to a class.  

Resource contains the field hourly rate, which is needed on both the Resource Assignment, and Time objects. Both are related to the Resource object.Ultimately the hourly rate field from the Resource Assignment, and Time objects are used in calculations on another object so a Rollup field is not an option.  

Here is how I envision it going:

 

trigger RAHourlyRate on Resource_Assignment__c(before insert, before update) {
     HourlyRate.Rate(/*pass the resource ID from the Resource Assignment Ojbect*/);
}
trigger TimeHourlyRate on Time(before insert, before update) {
     HourlyRate.Rate(/*pass the resource ID from the Time Management Ojbect*/);
}

public class HourlyRate{ 
  

  public static void Rate() { 
	/*Variable to receive the Resource ID passed from either the Resource assignment or Time Objects*/
   
    
    for(Resource r:[SELECT id, Hourly_Rate__c FROM Resource__c
            WHERE id = :Variable passed in from trigger]) {
      Resource_Assignment__c.hourly_rate__c = r.hourly_rate__c;
    } 
    Update Resource_Assignment__c;
    
  } 
} 

 

How do I pass the resource ID from the trigger to the class?
Will this work how I envision it or is this completely incorrect?

Thank you in advance for your help.

 

the_wolfthe_wolf

Hi austin_steve,

 

try this:

 

trigger RAHourlyRate on Resource_Assignment__c(before insert, before update) {
String ID_RESOURCE = trigger.new[0].id;
HourlyRate.Rate(ID_RESOURCE);
}
public class HourlyRate{ 


public static void Rate(String ID_RESOURCE) {

for(Resource r:[SELECT id, Hourly_Rate__c FROM Resource__c
WHERE id = : ID_RESOURCE ]) {
Resource_Assignment__c.hourly_rate__c = r.hourly_rate__c;
}
Update Resource_Assignment__c;

}
}
Austin_SteveAustin_Steve

Thank you for your input. I think I'm 90% there. In the code provided everything works (I think) except the Update Resource Assignment Hourly Rate  line. As I read through it again I understand why but not sure how to rectify the issue.  
So here is my next shot at it:

 

trigger RateForRA on RA__c (before insert, before update) {
	String ID_Resource = trigger.new[0].Id;
	HourlyRate.Rate(ID_Resource);		   
}

I think this looks correct and it seems to be passing the ID like I wanted, but in my class code I had previously been trying to update the Resource Assignment object, which I don't want to do in the class I want to do that in the Trigger. So below is what I think would be the correct way to do that.  I'm just not sure if I need something in the class to capture the value as I pass control back to the trigger then update the RA__c table.

 

 

public with sharing class HourlyRate {

    public Rate(String ID_Resource){
        Double Rt = 0;
        
        for (R__c r:[select id, hourly_rate__c
                      from R__c
                      where id = :ID_Resource]){
            Rt = r.hourly_rate__c;
        }
        return Rt;        
    }
}

 Again thank you for all your help

 

Steve

 

 

the_wolfthe_wolf

I don't understand very well the information described.

 

The main function you need is to update the record in the trigger?

 

If so, try this:

 

trigger RateForRA on RA__c (before insert, before update) {
String ID_Resource = trigger.new[0].Id;

// UPDATE RECORD: every field in a before trigger, can be updated in this way:
trigger.new[0].CustomField__c = ValueYouWant;

HourlyRate.Rate(ID_Resource);
}