4

I have several Workflow Rules + Field Updates in my QuoteLineItem object. This is not working well, since there is too much logic in Field Updates and I do not have full control on execution order, so I'm starting to move some of that logic to triggers.

I'm struggling with one of them. My field update is supposed to update a QuoteLineItem's field with a value obtained from its related PricebookEntry.UnitPrice upon creation (workflow rule criteria is Evaluate the rule when a record is created). So, my first thought was to convert that to a before insert trigger.

However, I need that lookup related field, but I don't have that field on the trigger.new records right away, but I also can't query it since those objects do not have an Id yet to use in the query (as in [SELECT Id, PricebookEntry.UnitPrice FROM QuoteLineItem WHERE Id IN :trigger.new]).

So, how can I get access to that lookup related field? Or is there a better approach than this before insert trigger to do what I want to?

5

You can just query the PricebookEntry records instead:

Set<Id> entryIds = new Set<Id>();
for (QuoteLineItem item : trigger.new) entryIds.add(item.PricebookEntryId);
Map<Id, PricebookEntry> entries = new Map<Id, PricebookEntry>([
    SELECT UnitPrice FROM PricebookEntry WHERE Id IN :entryIds
]);

for (QuoteLineItem item : trigger.new)
{
    PricebookEntry entry = entries.get(item.PricebookEntryId);
    if (entry != null)
    {
        // use entry data
    }
}
  • 2
    Oh thank you, that's right. Seems so obvious now :). – Mauricio Oliveira May 9 '17 at 18:30
  • @MauricioOliveira Glad I could help! Don't forget to accept this answer if it resolved your issue. :) – Adrian Larson May 9 '17 at 18:45
  • 1
    Was just waiting the cooldown to be able to accept it :) – Mauricio Oliveira May 9 '17 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.