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
Pritesh Khole 1Pritesh Khole 1 

Apex trigger for Renewal opportunity

Hi Team,

I am trying to create a renewal opportunity along with the product, based on some condition. I have written a trigger, however the opportunity is getting renewed but the products are not. Is there anything that I am missing. Require some help. Below is my code

trigger RenewalOpportunityProcess on Opportunity (after insert,after update) {
      Map<Opportunity,List<OpportunityLineItem>> OpportunityLineItemsMapping = new Map<Opportunity,List<OpportunityLineItem>>();

List<Opportunity> listOpp = new List <Opportunity>(); //List of Opportunity to store all the renewal Opportunities
     

    
    //Create Renewal Opportunity when stage is 'Closed Won ' and 'Create Future Renewal Opportunity' is Yes.
    for (Opportunity opp:Trigger.new){
        opp = [Select Name,StageName,AccountId, (SELECT Quantity, TotalPrice, PricebookEntry.Name, PricebookEntry.Product2.Family FROM OpportunityLineItems) from Opportunity where id=:opp.Id LIMIT 1 ];
        
        
         if(opp.StageName == 'Closed Won' && opp.AccountId != null && opp.Create_Future_Renewal_Opportunity__c == 'Yes' && opp.HasOpportunityLineItem == true)
         {          
             Opportunity oppNew = opp.clone(false,true);  //This clone method Creates a copy of the sObject record.
             oppNew.StageName = 'Qualification';
             oppNew.Name = opp.Name + ' - ' + 'RENEWAL';
             listOpp.add(oppNew);
             OpportunityLineItemsMapping.put(oppNew,opp.OpportunityLineItems.deepClone(false,false,false));
           
            }
        insert listOpp;
         
        
          
        }
    }
      
 
    
    


 
Best Answer chosen by Pritesh Khole 1
Steven NsubugaSteven Nsubuga
First to prevent recursion, create this class.
public class CheckRecursive { 
   public static boolean firstRun = true; 
}
Then update the trigger
trigger RenewalOpportunityProcess on Opportunity (after insert,after update) {
	
	if(CheckRecursive.firstRun){
		CheckRecursive.firstRun=false;
		
		List<OpportunityLineItem> OpportunityLineItemsList = new List<OpportunityLineItem>();

		Map<ID, Opportunity> oldOppIdNewOpp = new Map<ID, Opportunity>(); 

		//Create Renewal Opportunity when stage is 'Closed Won ' and 'Create Future Renewal Opportunity' is Yes.
		
		List<Opportunity> opps = [Select Name,StageName,AccountId, (SELECT Quantity, TotalPrice, PricebookEntry.Name, PricebookEntry.Product2.Family FROM OpportunityLineItems) from Opportunity where id IN:Trigger.newMap.keyset()];
		for (Opportunity opp:opps){
			
			 if(opp.StageName == 'Closed Won' && opp.AccountId != null && opp.Create_Future_Renewal_Opportunity__c == 'Yes' && opp.HasOpportunityLineItem == true)
			 {          
				 Opportunity oppNew = opp.clone(false,true);  //This clone method Creates a copy of the sObject record.
				 oldOppIdNewOpp.put(opp.Id, oppNew);
				 oppNew.StageName = 'Qualification';
				 oppNew.Name = opp.Name + ' - ' + 'RENEWAL';
				 OpportunityLineItemsMapping.put(oppNew,opp.OpportunityLineItems.deepClone(false,false,false));
			} 
		}
		insert oldOppIdNewOpp.values();
				
		for (Opportunity opp:opps){
			
			if(opp.StageName == 'Closed Won' && opp.AccountId != null && opp.Create_Future_Renewal_Opportunity__c == 'Yes' && opp.HasOpportunityLineItem == true) {          
				for (OpportunityLineItem oppLineItem :opp.OpportunityLineItems) {
					OpportunityLineItem tmp = oppLineItem.clone(false, true);
					tmp.OpportunityID = oldOppIdNewOpp.get(oppLineItem.OpportunityId).Id;

					OpportunityLineItemsList.add(tmp);
				}
			} 
		}
		insert OpportunityLineItemsList;
	}
}

 

All Answers

Steven NsubugaSteven Nsubuga
First to prevent recursion, create this class.
public class CheckRecursive { 
   public static boolean firstRun = true; 
}
Then update the trigger
trigger RenewalOpportunityProcess on Opportunity (after insert,after update) {
	
	if(CheckRecursive.firstRun){
		CheckRecursive.firstRun=false;
		
		List<OpportunityLineItem> OpportunityLineItemsList = new List<OpportunityLineItem>();

		Map<ID, Opportunity> oldOppIdNewOpp = new Map<ID, Opportunity>(); 

		//Create Renewal Opportunity when stage is 'Closed Won ' and 'Create Future Renewal Opportunity' is Yes.
		
		List<Opportunity> opps = [Select Name,StageName,AccountId, (SELECT Quantity, TotalPrice, PricebookEntry.Name, PricebookEntry.Product2.Family FROM OpportunityLineItems) from Opportunity where id IN:Trigger.newMap.keyset()];
		for (Opportunity opp:opps){
			
			 if(opp.StageName == 'Closed Won' && opp.AccountId != null && opp.Create_Future_Renewal_Opportunity__c == 'Yes' && opp.HasOpportunityLineItem == true)
			 {          
				 Opportunity oppNew = opp.clone(false,true);  //This clone method Creates a copy of the sObject record.
				 oldOppIdNewOpp.put(opp.Id, oppNew);
				 oppNew.StageName = 'Qualification';
				 oppNew.Name = opp.Name + ' - ' + 'RENEWAL';
				 OpportunityLineItemsMapping.put(oppNew,opp.OpportunityLineItems.deepClone(false,false,false));
			} 
		}
		insert oldOppIdNewOpp.values();
				
		for (Opportunity opp:opps){
			
			if(opp.StageName == 'Closed Won' && opp.AccountId != null && opp.Create_Future_Renewal_Opportunity__c == 'Yes' && opp.HasOpportunityLineItem == true) {          
				for (OpportunityLineItem oppLineItem :opp.OpportunityLineItems) {
					OpportunityLineItem tmp = oppLineItem.clone(false, true);
					tmp.OpportunityID = oldOppIdNewOpp.get(oppLineItem.OpportunityId).Id;

					OpportunityLineItemsList.add(tmp);
				}
			} 
		}
		insert OpportunityLineItemsList;
	}
}

 
This was selected as the best answer
Pritesh Khole 1Pritesh Khole 1
Thank you so much. This worked.