You need to sign in to do that
Don't have an account?
Jeff Bomar
create a query and place the results in a custom field
Trying to create an Apex trigger to query the price-book entries and place the result in a custom field can someone please tell me what i am doing wrong
get two problems Expression must be a list type: pricebookEntry
and a value cannot be stored to Hourly_Cost__c in type Custom_Programming__C
trigger updateCustomDev on Custom_Programming__c (after update) {
//SQL statement to lookup price
PricebookEntry sqlResult = [SELECT UnitPrice
FROM PricebookEntry
WHERE Product2.Name = 'Custom Development'
AND PriceBook2.Name='DAKCS'];
//save the returned SQL result inside the field
Custom_Programming__c.Hourly_Cost__c = sqlResult[0].UnitPrice;
}
get two problems Expression must be a list type: pricebookEntry
and a value cannot be stored to Hourly_Cost__c in type Custom_Programming__C
trigger updateCustomDev on Custom_Programming__c (after update) {
//SQL statement to lookup price
PricebookEntry sqlResult = [SELECT UnitPrice
FROM PricebookEntry
WHERE Product2.Name = 'Custom Development'
AND PriceBook2.Name='DAKCS'];
//save the returned SQL result inside the field
Custom_Programming__c.Hourly_Cost__c = sqlResult[0].UnitPrice;
}
Hello Jeff!
First off, sqlResult has to be a List<PricebookEntry> since you aren't limiting how many records can be fetched:
Since your trigger is on after update, at the end you would have to use an "update", or change it to "(before update)".
If you change it to before update you don't have to trigger the same trigger again, but if there is a reason for it to be an after update trigger, then leave it as is, and just update the record at the end.
Your trigger as an "before update"-trigger:
Your trigger as an "after update"-trigger:
All Answers
Hello Jeff!
First off, sqlResult has to be a List<PricebookEntry> since you aren't limiting how many records can be fetched:
Since your trigger is on after update, at the end you would have to use an "update", or change it to "(before update)".
If you change it to before update you don't have to trigger the same trigger again, but if there is a reason for it to be an after update trigger, then leave it as is, and just update the record at the end.
Your trigger as an "before update"-trigger:
Your trigger as an "after update"-trigger: