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

Trigger to clone opportunity and opportunity products

I have a very simple trigger on my opportunity object that will clone an opportunity once the status = Win. I need to add some functionality and have it clone the opportunity products as well. Is this not the same as a deep clone? I tried adding an additional parameter to the Clone function but this did not work. Can anyone help with how I would get the products cloned with the opportunity. Here is code:

trigger RenewalOpportunity on Opportunity (after update) { for (Integer i = 0; i <; i++) { System.debug('checking opportunity ' + i); System.debug('old.isWon: ' + Trigger.old[i].isWon); System.debug('new.isWon: ' +[i].isWon); if ([i].isWon == true && Trigger.old[i].isWon == false) { Date endDate =[i].CloseDate.addDays(365); System.debug('updating opportunity ' + i); Opportunity renewalOpp =[i].clone(false); renewalOpp.Name = 'Renewal - '+[i].Name; renewalOpp.closedate = endDate; renewalOpp.stagename = 'D - Relationship Management'; insert renewalOpp; if ([i].Name.startsWith('Renewal - ')) { renewalOpp.Name =[i].Name; update renewalOpp; } System.debug('renewal opportunity inserted' + i); System.debug(' ' + renewalOpp.ID); System.debug('TriggerNew[i].id: ' +[i].ID); if ([i].isWon == true && Trigger.old[i].isWon == false) { System.debug('Associating generated opportunity as child of closed->won opportunity'); Opportunity newOpp = [select ID from Opportunity where id =[i].ID]; newOpp.Renewal_Opportunity__c = renewalOpp.ID; update newOpp; } } } }



Deep clone only clones the current object, not any of it's child objects.

You will have to


- Query for the Opportunity Products

- Deep clone each one and store it in a list of Opportunity Products

- insert your cloned Opportunity

- update the deep cloned Opportunity Products with the new cloned Opportunity Id

- insert your cloned Opportunity Products


Did you get this working and if so would you mind posting the final code?  I need to do something similar but also add in contact roles to the mix.




Nirmal ChristopherNirmal Christopher

try something like this

Mrsamandahowell wrote:

Did you get this working and if so would you mind posting the final code?  I need to do something similar but also add in contact roles to the mix.




trigger renewalopportunity on Opportunity (after update) {
public list<id> oppid=new list<id>();
integer i;
//getting Opportunity Id
//geting current opportunity
public List<Opportunity>oppcurrent=new list<Opportunity>();
oppcurrent=[select id,Name,Renewal_Date__c from opportunity where id =:oppid];
//getting current opp contact role
public list<OpportunityContactRole> OCR = [SELECT ContactId,Id,IsDeleted,IsPrimary,OpportunityId,Role FROM OpportunityContactRole where OpportunityId IN :oppid];
//inserting clone opportunity
if(o1.Renewal_Date__c!=null && o1.StageName=='Closed Won' ){
//creating renewal opp
opportunity newrec=new Opportunity(Name = 'Renewal Opportunity-'+oppcurrent[0].Renewal_Date__c , Opportunity__c= oppcurrent[0].id,stageName='closed won',
AccountId = O1.AccountId, OwnerId =o1.OwnerId, );
insert newrec;
system.debug('+++New opp+++'+newrec);
system.debug('+++New opp id+++';
//creating contact role from parent opportunity
for (i = 0; i < OCR.size(); i++)
//Create new contact role with new sub opportunity
OpportunityContactRole NewOCR= new OpportunityContactRole(ContactId=OCR[i].ContactId,OpportunityId=newRec.ID,

insert NewOCR;
system.debug('+++New OCR+++'+NewOCR);