Search code examples
classtriggersapexapex-codeeventtrigger

Referencing a Order sObject used in a trigger in a class to pull line items from Order Products sObject for that order in a seperate class


I have a trigger that is tripped and a portion of the code runs whenever the Order sObject's Status field is changed from draft to active. I am trying to write a class that only pulls the line items from that order from the Order Product sObject. What I'm looking for is not a direct answer as I am trying to learn all the ins and outs of apex programming. If any one has an example they can give me on how I would go about referencing the Order sObject that tripped the trigger and pull only the line items for that order from the Order Product sObject it would be much appreciated. I am trying to achieve this without doing a SOQL query.


Solution

  • You can access the order (or orders as triggers should be able to handle bulk actions) that initiated the trigger with the Trigger.new or Trigger.newMap; context variables the new variable is a list of sObjects the trigger is for and the newMap is similar but a Map with the records Ids as the keys and the objects as the values. The only catch is newMap is only available on trigger types where the records will have IDs (not before Insert). Now to retrieve the order products you will need a SOQL Query using the orders Ids (Remember to handle bulk actions eg. no Queries in for loops). Hope this helps