0

I have a trigger that creates a new quote when a new business opportunity is created, it works very nicely however I have requirement to create multiple quotes when the opportunity is created. 4 quotes for each new opp. Can someone help me achieve this. My current trigger is below:

trigger CreateQuote on Opportunity (after insert) {
    List<Id> oppIdsList = new List<Id>();
    List<Quote> quoteList = new List<Quote>();
    for (Opportunity o : Trigger.new) {
        oppIdsList.add(o.id);
    }
    Opportunity[] oppList = [SELECT
                                name,
                                id,
                                account.billingStreet,
                                account.billingCity,
                                account.billingState,
                                account.billingCountry
                            FROM Opportunity
                            WHERE id IN :oppIdsList and Type = 'New Client'];
    for (Opportunity o : oppList) {
        Quote q = new Quote();
        q.name = 'Quote-' + o.name;
        q.opportunityId = o.id;
        q.billingStreet = o.account.billingStreet;
        q.billingCity = o.account.billingCity;
        q.billingState = o.account.billingState;
        q.billingCountry = o.account.billingCountry;
        q.ExpirationDate = date.today()+60;
        quoteList.add(q);
    }
    insert quoteList;
}
1
  • What's wrong with creating 4 quotes in the for (Opportunity o : oppList) loop?
    – BarCotter
    Commented Mar 24, 2015 at 15:56

2 Answers 2

1

This would be pretty straightforward to implement four quotes instead of 1.

Essentially you'd want to repeat the steps you already have within your iterator for loop once per quote.

for (Opportunity o : oppList) {
 //start here
    Quote q = new Quote();
    q.name = 'Quote-' + o.name;
    q.opportunityId = o.id;
    q.billingStreet = o.account.billingStreet;
    q.billingCity = o.account.billingCity;
    q.billingState = o.account.billingState;
    q.billingCountry = o.account.billingCountry;
    q.ExpirationDate = date.today()+60;
    quoteList.add(q);
  //repeat to here
}

A simple way would be to do like this:

for (Opportunity o : oppList) {
    Quote q1 = new Quote();
    q1.name = 'Quote-' + o.name;
    q1.opportunityId = o.id;
    q1.billingStreet = o.account.billingStreet;
    q1.billingCity = o.account.billingCity;
    q1.billingState = o.account.billingState;
    q1.billingCountry = o.account.billingCountry;
    q1.ExpirationDate = date.today()+60;
    quoteList.add(q1);

    Quote q2 = new Quote();
    q2.name = 'Quote-' + o.name;
    q2.opportunityId = o.id;
    ...etc.
    quoteList.add(q2);

    //...repeat ad nauseum

}

For the sake of DRY, you'd probably want to make this a bit more friendly by writing yourself a little helper method in a class somewhere with the params you want to set differently for each quote:

public class MyHelper{
  public static Quote createQuoteFromOpp(String NameStr, Opportunity o){
    Quote q = new Quote();
    q.opportunitId =  o.id;
    q.name = NameStr + ' ' + o.name;
    //etc...
    return q
}

Then you could just invoke it however many times you want...whether several times manually...in a loop...etc.

for (Opportunity o : oppList) {
    quoteList.add(MyHelper.createQuoteFromOpp('Quote 1', o));
    quoteList.add(MyHelper.createQuoteFromOpp('Quote 2', o));
    quoteList.add(MyHelper.createQuoteFromOpp('Quote 3', o));
    quoteList.add(MyHelper.createQuoteFromOpp('Quote 4', o));
}
1

Your trigger looks reasonably bulkified (which means will not break under bulk data loads - doesn't do DML or query in loops).

What you need is an additional for() loop inside your for (Opportunity o : oppList) loop:

for (Integer i = 0; i < 4; i++) { [create quote & add to list here] }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .