Created
April 3, 2018 23:28
-
-
Save forcethesales/86c26dd1b2983db0a2fae0e9fb33ab65 to your computer and use it in GitHub Desktop.
Quote Before Trigger that gets Value from Opp and Writes it to the Quote
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//written by Bobby Watson | |
public void OnBeforeInsert(Quote [] newQuotes){ | |
Set<Id> opportunitiesToQuery = new Set<Id>(); | |
// Loop over the new quotes and grab the opportunities they are associated to. | |
for (Quote q : newQuotes) { | |
opportunitiesToQuery.add(q.OpportunityId); | |
} | |
// Now we have a collection of opportunity IDs to query. So let's query for them. | |
// We'll store the results in a Map keyed by Opportunity ID so we can grab the correct one for each quote. | |
Map<Id, Opportunity> opps = new Map<Id, Opportunity([SELECT Id, EnergyEngineer__c FROM Opportunity WHERE id =:opportunitiesToQuery]); | |
for (Quote q : newQuotes) { | |
// Since we already did a query for all opportunities, now we just need to grab the one for the current quote. | |
Opportunity o = opps.get(q.OpportunityId); | |
// We should check to make sure an opportunity record was actually found first. | |
if (o != null) { | |
q.Energy_Engineer__c = o.EnergyEngineer__c; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment