You need to sign in to do that
Don't have an account?
Mitch McConnell
The method being called is as follows:
pre insert trigger question
I am reviewing some code that I inherited :-)
It uses a "before" trigger as follows. It is trying to take a variable from the Quote and
copy it to the OpportunityLineItem.
trigger OpportunityProductTrigger on OpportunityLineItem (before insert, before update) { if(Trigger.isBefore){ OpportunityProductServices.addParentSortOrderNumber(trigger.new); } }
The method being called is as follows:
public class OpportunityProductServices{ // Populating Base OpportunityLine Item's Sort Order number on Add-on OLI's public static void addParentSortOrderNumber(List<OpportunityLineItem> lst_OLI){ Set<String> set_OpportunityIds = new Set<String>(); // Creating Set of Opportunity Ids for which Opportunity Lineitem is created or updated for(OpportunityLineItem var : lst_OLI){ set_OpportunityIds.add(var.OpportunityId); } Map<Id, Map<Id, QuoteLineItem>> map_Opprtunity2Quote = new Map<Id, Map<Id, QuoteLineItem>>(); // Getting List of QLI for Opportunity ids which has Quote Quote Synced for(QuoteLineItem var : [Select Id, SortOrder, Base_OLI_Id__c, Quote.OpportunityId from QuoteLineItem WHERE Quote.isSyncing=true and Quote.OpportunityId IN : set_OpportunityIds]){ if(map_Opprtunity2Quote.containsKey(var.Quote.OpportunityId)){ map_Opprtunity2Quote.get(var.Quote.OpportunityId).put(var.Id, var); }else{ Map<Id, QuoteLineItem> map_QuoteLine = new Map<Id, QuoteLineItem>(); map_QuoteLine.put(var.Id, var); map_Opprtunity2Quote.put(var.Quote.OpportunityId, map_QuoteLine); } } for(OpportunityLineItem var : lst_OLI){ Map<Id, QuoteLineItem> map_QuoteLine = map_Opprtunity2Quote.get(var.OpportunityId); for(QuoteLineItem QLI : map_QuoteLine.values()){ // Putting Sort Number on Custom Field of OLI if(var.SortOrder == QLI.SortOrder && QLI.Base_OLI_Id__c != null){ var.Base_OLI_Sort_Number__c = map_QuoteLine.get(QLI.Base_OLI_Id__c).sortOrder; } } } } }
My question is this:
Since there are no DML operations being performed in the called method, does this
code *do* anything?
Is the "trigger.new" that is passed in directly from the trigger to the method passed by reference, and
therefore setting Base_OLI_Sort_Number__c actually sets it somehow?
Thanks,
Mitch
OpportunityProductTrigger fire on OpportunityLineItem whenever you perform insert/update through UI or through data loader.
Example:
MyObject {Name = 'Smith'} is the record I am inserting through UI or Apex Data Loader.
In before trigger you can modify the record info. say I am changing to MyObject {Name = 'Will Smith'}
Note: I am modifying the record before the DML operation completes hence it will perform the DML operation with my updated changes.
This is the reason why in before insert/update if you are making changes on the same object on which you are creating the trigger no need of using the DML explicitly.
Above code will make changes to the Base_OLI_Sort_Number__c field.
------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
All Answers
OpportunityProductTrigger fire on OpportunityLineItem whenever you perform insert/update through UI or through data loader.
Example:
MyObject {Name = 'Smith'} is the record I am inserting through UI or Apex Data Loader.
In before trigger you can modify the record info. say I am changing to MyObject {Name = 'Will Smith'}
Note: I am modifying the record before the DML operation completes hence it will perform the DML operation with my updated changes.
This is the reason why in before insert/update if you are making changes on the same object on which you are creating the trigger no need of using the DML explicitly.
Above code will make changes to the Base_OLI_Sort_Number__c field.
------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.