How does the QuoteLineItems Generated from the OpportunityLineItems, Can I add Fields that It can copy? How? I'm looking for a way to add fields to copy from the opportunity items to the quote items, or a way to quite that auto filling process, and do a trigger that does it.

The idea would be a

for each oppItem in Opportunity:
    new quotelineitem = generateFrom(oppItem)

(this is of course not apex, but still, I think you can get the feeling of what I'm attempting).

Any ideas?


QuoteLineItems and OpportunityLineItems are not directly related. Look at this ERD for Product and Schedule Objects from the SOAP API Developer Guide. You can grab the idea of how these object connected from there.

I assume you want to copy custom fields over to QuoteLineItems, because otherwise you could utilize the Standard Quote Syncing feature.

Now that the picture is clear, you can copy any OpportunityLineItems data to QuoteLineItems. Basically one opportunity can have many OpportunityLineItems. And it can have many Quotes which, in turn, can have many QuoteLineItems each. So to be able to link them, i.e. establish a kind of 1-to-1 connection between OpportunityLineItem and QuoteLineItem, you need work with the compaund key "ProductId + Quantity" fields which is present in the both entities. Such a key would allow uniquely identify line items and relate with each other.

So a pseudo code for this solution would look like:

Opportunity opp = getOpportunity();
List<Quote> quotes = opp.Quotes;
Quote qt = quotes[0]; // this is just an example, you can choose any quote depending on your requirements.

Map<String,QuoteLineItem> qliByProductAndQuantity = new Map<String,QuoteLineItem>();

for (QuoteLineItem qli : qt.QuoteLineItems)
    qliByProductAndQuantity.put(qli.ProductId+''+qli.Quantity, qli);

// Finally copy over the data
List<OpportunityLineItem> qlis = new List<OpportunityLineItem>();
for (OpportunityLineItem oli : opp.OpportunityLineItems)
   QuoteLineItem qli = qliByProductAndQuantity.get(opp.ProductId+''+opp.Quantity);
qli.Custom_Field__c = opp.Custom_Field__c;


update qlis;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.