function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Linda 98Linda 98 

trigger to clone opportunity with opportunity products

How to clone opportunity with opp products in a trigger.
I have all my functionality set in trigger .I want Line items to be cloned to new cloned opportunities.

Please help
Best Answer chosen by Linda 98
Linda 98Linda 98
i achieved this by below way:

after inserting new opportunity list,

//inserting new opportunities.
insert Oppstoinsert;

//Query line items from main opportunity and save it in ProductList
ProductList=[Select opportunityId,quantity,UnitPrice,ListPrice,pricebookentryid,Discount,ServiceDate,Description from opportunitylineitem where opportunityId=:op.id];

       for(opportunity opnew:oppstoinsert){
          
           for(opportunitylineitem Product:ProductList){
           Opportunitylineitem NewItem = new opportunitylineItem();
           NewItem.opportunityId=opnew.id;
           NewItem.quantity=Product.quantity;
           NewItem.UnitPrice=Product.UnitPrice;
           NewItem.pricebookentryid=Product.pricebookentryid;
           linitem.add(NewItem);   
       }
       }
       insert linitem;
}

All Answers

AmulAmul
here you can create a Custom Clone detail page button that button will be associated with VF Page. and put entire logic in Apex class.
Linda 98Linda 98
But i have a trigger conditions to be executed like before insert, before update. And other conditions. Can i not do something in trigger which will clone products too along with opportunities? Thanks
Linda 98Linda 98
i achieved this by below way:

after inserting new opportunity list,

//inserting new opportunities.
insert Oppstoinsert;

//Query line items from main opportunity and save it in ProductList
ProductList=[Select opportunityId,quantity,UnitPrice,ListPrice,pricebookentryid,Discount,ServiceDate,Description from opportunitylineitem where opportunityId=:op.id];

       for(opportunity opnew:oppstoinsert){
          
           for(opportunitylineitem Product:ProductList){
           Opportunitylineitem NewItem = new opportunitylineItem();
           NewItem.opportunityId=opnew.id;
           NewItem.quantity=Product.quantity;
           NewItem.UnitPrice=Product.UnitPrice;
           NewItem.pricebookentryid=Product.pricebookentryid;
           linitem.add(NewItem);   
       }
       }
       insert linitem;
}
This was selected as the best answer
parth jollyparth jolly
Hi ,
Please can you post full code 

Thanks & Regards 

Paarth Jolly