You need to sign in to do that
Don't have an account?

Updating a price based on lookup field
I have two objects:
AccountPriceBookEntry__c and TimeSheetEntry__c
- AccountPriceBookEntry__c has a Price__c field that stores the price of an Employee's rate for that particular account
- TimeSheetEntry__c has a Rate__c field that should store the price from the AccountPriceBookEntry__c
- TimeSheetEntry__c is where an employee fills out a time sheet and selects an AccountPriceBookEntry__c from a lookup field
- I need the Time Sheet Entry's Rate__c field to be updated based on the Price__c field in the other object
Code:
trigger updateRate on TimeSheetEntry__c (after insert, after update) { List<String> TimeRate = new List<String>(); for (Integer i = 0; i < Trigger.new.size(); i++) { TimeRate.add(Trigger.new[i].Account_Price_Book_Entry__c); } List<AccountPriceBookEntry__c> rateList = new List<AccountPriceBookEntry__c>([Select Price__c from AccountPriceBookEntry__c where Id in :TimeRate]); for (Integer i = 0; i < Trigger.new.size(); i++) { if (Trigger.new[i].Account_Price_Book_Entry__c != null) { Trigger.new[i].Rate__c = rateList[i].Price__c; } } }
Am I way off base here?
I get an exception error that says
Code:
Error:Apex trigger updateRate caused an unexpected exception, contact your administrator: updateRate: execution of AfterUpdate caused by: System.Exception: Record is read-only: Trigger.updateRate: line 16, column 28
Cheers!
I found the issue: the trigger needs to be set to "before insert/update" instead of:
trigger updateRate on TimeSheetEntry__c (after insert, after update) {