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
sgoremasgorema 

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; } } } }

 

WhyserWhyser

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

SFDCDevQASFDCDevQA

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.

 

Thanks,

Amanda

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.

 

Thanks,

Amanda


trigger renewalopportunity on Opportunity (after update) {
public list<id> oppid=new list<id>();
integer i;
//getting Opportunity Id
for(opportunity o:trigger.new){
oppid.add(o.id);
}
system.debug('+++oppid+++'+oppid.size());
system.debug('+++oppid+++'+oppid[0]);
//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];
system.debug('+++oppid+++'+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);
system.debug('+++OCR+++'+OCR.size());
//creating contact role from parent opportunity
if(OCR.size()>0)
{
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,
Role=OCR[i].Role,IsPrimary=OCR[i].IsPrimary);

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

}
}
}
}