+ 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 < Trigger.new.size(); i++) { System.debug('checking opportunity ' + i); System.debug('old.isWon: ' + Trigger.old[i].isWon); System.debug('new.isWon: ' + Trigger.new[i].isWon); if (Trigger.new[i].isWon == true && Trigger.old[i].isWon == false) { Date endDate = Trigger.new[i].CloseDate.addDays(365); System.debug('updating opportunity ' + i); Opportunity renewalOpp = Trigger.new[i].clone(false); renewalOpp.Name = 'Renewal - '+ Trigger.new[i].Name; renewalOpp.closedate = endDate; renewalOpp.stagename = 'D - Relationship Management'; insert renewalOpp; if (Trigger.new[i].Name.startsWith('Renewal - ')) { renewalOpp.Name = Trigger.new[i].Name; update renewalOpp; } System.debug('renewal opportunity inserted' + i); System.debug('renewalOpp.id: ' + renewalOpp.ID); System.debug('TriggerNew[i].id: ' + Trigger.new[i].ID); if (Trigger.new[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 = :Trigger.new[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
for(opportunity o:trigger.new){
//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
for(Opportunity o1:trigger.new){
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,closedate=system.today() );
insert newrec;
system.debug('+++New opp+++'+newrec);
system.debug('+++New opp id+++'+newrec.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);