My scenario:

When i insert opportunity line-item on Parent Opportunity...opportunity line-item are also created on child opportunity(same as line-item on Opportunity).If there are 2 child opportunities then same opportunity line-items records will be created on both the child opportunity.

My problem is i have a button on opportunity when clicked on that a VF page opens and shows opportunity line-item records(editable) of parent opportunity.if i update any line-item record of parent opportunity then the child-opportunity line-item record should also be updated on all the child opportunities

As IDS of both opportunity line items records are different on child and parent opportunity. How to update child opportunity(opportunity line-item record) id parent opportunity opportunity line-item records are updated.

I am slightly confused..how should i do this..

I am able to able update the parent opportunity (opportunity line-item records). But how to update child-opportunity opportunity line-item records. Also I am able to get the ids of child opportunity opportunity line-item records.I dont know how to use them...Please help... UPDATED CODE

>  for(opportunity o:oppchildlist)
>          {
>              for(opportunitylineitem ol : opllist)
>              {
>                  opline=new opportunitylineitem();
>                  opline.opportunityid=o.id;
>                  opline.PricebookEntry=ol.PricebookEntry;
>                  opline.PricebookEntryid=ol.PricebookEntryid;
>                  opline.Business_Type__c=ol.Business_Type__c;

>                  childoppitem.add(opline);
>              }
>          }

Hey i am thinking of implementing the fourth solution..but i have doubt how do i stamp the value of the Parent OpportunityLineItem at create.i mean as the parent opportunity line item record is not created so we dont have the id of that record.so how do i save the value of the id in child opportunity line item record.


2 Answers 2


A few ways I can think of:

One - always delete the line items on child opportunity and create afresh on every change on the parent, as a copy from the parent. Too much DML.

Two - create a custom formula field on OpportunityLineItem which is a composite key, eg PriceBookEntryId+Quantity, which will evaluate to the same on parent and child OpportunityLineItems and therefore you can match using the formula field composite key.

Three - (Recommended) If you can be sure that a Product is added only once, then you can match on the PriceBookEntryId, because this will be the same on Parent and Child Opportunity Line Items. This sounds the most promising provided the precondition is met.

Four - create a hidden text field on OpprtunityLineItem, into which you stamp the value of the Parent OpportunityLineItem at create. Then for every subsequent change you can match by the parent OpportunityLineItem Ids that are changing and accordingly update all related child OpportunityLineItem Ids.

Sample Code for Solution Four :

Create a hidden field ParentLineItemId__c on OpportunityLineItem (Text, 18) Then in your inner for loop, you can set this

opline.ParentLineItemId__c=ol.Id; //set the Parent OLI Id on the Child OLI
  • Sweet! Cheers mate! Commented Nov 17, 2012 at 7:05
  • Hey i am thinking of implementing the fourth solution..but i have doubt how do i stamp the value of the Parent OpportunityLineItem at create..i have updated the code..can you check..
    – Mik
    Commented Nov 17, 2012 at 7:35
  • Have you created a new field on the OpportunityLineItem called the ParentOpportunityLineItemId__c ? You will need to set this field with the value of the Parent Opportunity's Line Item ids. Commented Nov 17, 2012 at 7:47
  • Yes I have created the field... But how to set the value of parent line item is.
    – Mik
    Commented Nov 17, 2012 at 8:27
  • I've added it as an edit in my answer, towards the end, we could move to chat if more comments ensue. Commented Nov 17, 2012 at 8:30

If you have Parent Oppurtunity Id then obviously you will be able to get all child oppurtunities .Now query all line items from the Oppurtunity Id(Parent and Child) and you can update all line items .

You must log in to answer this question.

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