You need to sign in to do that
Don't have an account?
ckellie
Copying more than one item
I have written a trigger to copy the opportunity line item as a quote line item, but the trigger is only copying one record, not all of the opportunity line items attached to the opportunity. How do I add a loop to copy as many opportunity line items to quote line items as are attached to the opportunity?
Trigger:
trigger NewQuoteLineItems on Quote (After Insert) { Set<Id> qid = new Set<Id>(); for(Quote q : Trigger.new){ System.debug('**** 0 q id : '+q.Opportunityid); qid.add(q.opportunityid); } System.debug('****1 : q Id size '+ qId.size()); List<quote> qu = new List<Quote>([select id, opportunityid from quote where opportunityid in:qid]); List<Opportunity> o = new List<Opportunity>([select id, Pricebook2Id from opportunity where id in:qid]); List<OpportunityLineItem> oli = new List<OpportunityLineItem>([select id, opportunityid, pricebookentryid, Quantity, UnitPrice from OpportunityLineItem where opportunityid = :qid]); System.debug('****2 : q Id size '+ qId.size()); List<QuoteLineItem> qli = new List<QuoteLineItem>(); System.debug('****2 : qli Id size '+ qli.size()); for(Quote quote : System.Trigger.new){ if(Quote.id != null) { qli.add(New QuoteLineItem ( quoteid = qu[0].id, pricebookentryid = oli[0].pricebookentryid, UnitPrice = oli[0].UnitPrice, Quantity = oli[0].Quantity)); } } insert qli; }
Thank you
Well, what is supposed to be happening is equivalent to:
List<OpportunityLineItem> x;
x = oliMap.get(oli.OpportunityId);
x.add(oli);
oliMap.put(oli.OpportunityId, x);
So, perhaps it's confused by putting it all on one line? Or maybe something isn't declared properly? Best, Steve.
All Answers
So I'm getting that you want an after insert trigger on Quote that, for each Quote, takes all the OLI's for the related Opp and inserts them as QLI's.
Perhaps this is what you want. Note that I'm just typing it in freehand and so I haven't tried to compile it. I'm sure there's probably a syntax/spelling error or two in there, but hopefully this gives you the idea.
Note that doing the whole Map creation thing isn't strictly needed. instead of that you could just iterate throuh the entire set of OLI's for each quote and see if the Opportunity ID's match. Of course that's pretty inefficient for larger numbers of records.
Best, Steve.
Steve,
Thanjk you for the code. I have been studying the code and have been able to further adapt the code, except for this line:
where I am recieving this error:
What does the error mean and why? how do I solve the error?
Thank you
Well, what is supposed to be happening is equivalent to:
List<OpportunityLineItem> x;
x = oliMap.get(oli.OpportunityId);
x.add(oli);
oliMap.put(oli.OpportunityId, x);
So, perhaps it's confused by putting it all on one line? Or maybe something isn't declared properly? Best, Steve.
Thank you. Separating the code into multiple lines was the key. I also understand more about maps through your help. Thank you Steve
oliMap.put(oli.OpportunityId, oliMap.get(oli.OpportunityId).add(oli));
By
oliMap.put(oli.OpportunityId, oliMap.get(oli.OpportunityId));