You need to sign in to do that
Don't have an account?
Help with a trigger. Accessing a related parent record values to populate child record fields
I have a trigger on a custom object called "Time_Entry__c" that; when a record is created and *IF* there is a related contract associated with the Time Entry, it looks up the rates set-out on that contract record and uses the rates listed in the parent (Contract) record.
Rate_1__c (currency field)
Rate_2__c (currency field)
The look-up field on the Time_Entry__C object is called Contract__c
The related object is the Contract object in SF: Contract__r
Contract__r has the two currency fields.
CustomRate1__c
CustomRate2__c
What I need is to have my trigger, look up the parent contract (if there is a value in Contract__c). and pull the two rates CustomRate1__c and CustomRate2__c and populate them into the Time Entry records Rate_1__c and Rate_2__c respectively.
Can anyone help with code for this trigger???
Let me give it a go:
Haven't really checked for syntax errors though, so let me know if it works.
All Answers
Let me give it a go:
Haven't really checked for syntax errors though, so let me know if it works.
Thanks for this!
Here is my full code using your solution: However, I get the following error when I try to save the Time Entry Record: In this case line 33 in my test record is running the correct line of code, but it is telling me that the TE.Rate__c is read only?? Any ideas?
It looks like it's related to you changing it from "before insert/update" to "after insert/update". In an after insert/update trigger, you're not allowed to change field values in Trigger.new context objects (the Apex Code Developer's Guide has a table showing what you can and can't do to context variables with different types of triggers).
Is there any reason why you prefer this code to be triggered after the insert or update?
Nope - I realized the mistake. This worked - You're the best ;)
Hugo
Is this trigger only useful with lookups or can it be used with master detail related lookup as well? how would the code coverage be done?