Trying to create a trigger, please see use case below. I'm extremely novice so I'm sure the pseudocode might not make perfect sense, but it's as close as I could get.
1: Objects: Account, Opportunity, Quote
2: A handful of Accounts have Record Type = 'Partner'
3: Quote has a custom lookup relationship to Account filtered by 'Partner' Record Type
4: When a User checks a box on an Opportunity, need to create one Quote for EACH 'Partner'Account
5: So 1 Account, 1 Opportunity, and maybe 10 Quote records all under the Opportunity, because there were 10 Accounts flagged as Partners…each Quote is related to each separate Partner Account
Pseudo code:
trigger CreateQuotes on Opportunity__c (after insert) {
List<Accounts> Accounts = new List<Accounts>()
where Account.RecordType == 'Partner';
for(Opportunity o : trigger.new)
{
Quote__c Quote = new Quote__c ();
Quote.Opportunity = o.id;
Quote.Name = 'testName';
Quotes__c.add(Quotes)
//and somehow increment through the above list of Accounts and create 1 Quote related to each Partner Account
}
insert Quotes;
}
Updated trigger with error: Error: Compile Error: Expression cannot be assigned at line -1 column -1
"
trigger CreateQuotes on Opportunity (after insert) {
List<Account> actList = [select Id from Account where RecordType.DeveloperName = 'Partner'];
list<NewQuote__c> quoteList = new list<NewQuote__c>();
for(Opportunity o : trigger.new)
{
NewQuote__c quote = new NewQuote__c ();
NewQuote__c.Opportunity__r.Id = o.id;
NewQuote__c.Name = 'testName';
quoteList.add(quote);
if (o.Needs_Partner_Quotes__c){
for(Account act: actList){
quote = new NewQuote__c(Opportunity__c = o.id, Account__c=act.Id, Name='testName');
quoteList.add(quote);
}
}
}
insert quoteList;
}
Best Answer
Your pseudo-code is actually pretty close. Here's how I'd approach this:
Note that the order is designed to be structured for maximum efficiency (e.g. no wasted queries, or iterating over Trigger.new more than once).