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
iSqFt_ADiSqFt_AD 

How to Add Dynamic Year into Opportunity Name during Opportunity Clone Trigger??

I have pieced together  a trigger in order to clone a Closed Won opportunity. The one part I do not understand is that I want the new Opportunity's name to be in the format of "Opp Name - Renewal - Year" with Year being the year after the current one. So if I Closed Won an Opportunity today (2012), then the cloned opportunity name would become "Opportunity Name - Renewal - 2013".

 

I have the Name - Renewal part but what do I need to add after it to get the dynamic year?

 

 newOpp.Name=o.Name + ' - Renewal';

 


Thank you!

TheIntegratorTheIntegrator

Have a temp variable to compute the next year, then add it in your assignment statement

 

Integer tYear = o.CloseDate.addYears(1).year();
newOpp.Name = o.Name+' - Renewal - '+ tYear;

 

iSqFt_ADiSqFt_AD

I guess I should have put a disclaimer that I took an existing trigger and made some mods to it to make it work. I am not sure exactly where your Integer line would fall. Here is the full trigger:

 

trigger CloneClosedWonOpportunity on Opportunity (after update) {

public List<Opportunity> insertOppList = new List<Opportunity>();

 for (opportunity o : trigger.new)
 {
  if (o.StageName=='Closed Won' && o.StageName!=trigger.oldMap.get(o.id).StageName)
  {
   opportunity newOpp =  o.clone(false, true);
   newOpp.Name=o.Name + ' - Renewal - ' + tYear;
   newOpp.RecordTypeID  = '012600000009BrI';
   newOpp.StageName  = 'NOT CONTACTED';
   newOpp.CloseDate = o.CloseDate +365;
   insertOppList.add(newOpp);
  }
 }
 
 if (!insertOppList.IsEmpty()) 
    insert insertOppList;
}

 

Where do I input:

 

Integer tYear = o.CloseDate.addYears(1).year();

 

Naidu PothiniNaidu Pothini
trigger CloneClosedWonOpportunity on Opportunity (after update) 
{
	public List<Opportunity> insertOppList = new List<Opportunity>();
	for (opportunity o : trigger.new)
	{
		if (o.StageName=='Closed Won' && o.StageName!=trigger.oldMap.get(o.id).StageName)
		{
			opportunity newOpp =  o.clone(false, true);
			Integer tYear = o.CloseDate.addYears(1).year();
			
			newOpp.Name=o.Name + ' - Renewal - ' + tYear;
			newOpp.RecordTypeID  = '012600000009BrI'; // not a good idea to use hardcoded Ids
			newOpp.StageName  = 'NOT CONTACTED';
			newOpp.CloseDate = o.CloseDate +365;
			insertOppList.add(newOpp);
		}
	}
	 
	if (!insertOppList.IsEmpty()) 
	{
		insert insertOppList;
	}
}

 

Try this.

iSqFt_ADiSqFt_AD

Thank you Naidu. 

 

What would you use in place of the hard coded ID? I do not want to use the name and the cloned opportunity will always change to that record type (different from the opportunity that is being cloned).

 

Once I verify your update works I will mark it as the solution.

 

Thanks again!

Naidu PothiniNaidu Pothini
trigger CloneClosedWonOpportunity on Opportunity (after update) 
{
        public String recTypeId = [SELECT Id, Name FROM RecordType WHERE Name = 'xxxxxxx'].Id;   // Replace with the name of the RecordType
	public List<Opportunity> insertOppList = new List<Opportunity>();
	for (opportunity o : trigger.new)
	{
		if (o.StageName=='Closed Won' && o.StageName!=trigger.oldMap.get(o.id).StageName)
		{
			opportunity newOpp =  o.clone(false, true);
			Integer tYear = o.CloseDate.addYears(1).year();
			
			newOpp.Name=o.Name + ' - Renewal - ' + tYear;

			newOpp.RecordTypeID  = recTypeId;  

			newOpp.StageName  = 'NOT CONTACTED';
			newOpp.CloseDate = o.CloseDate +365;
			insertOppList.add(newOpp);
		}
	}
	 
	if (!insertOppList.IsEmpty()) 
	{
		insert insertOppList;
	}
}

 Try this.

iSqFt_ADiSqFt_AD

How much more complicated is it to cause this trigger to also clone the products from the Closed Won opportunity?

Naidu PothiniNaidu Pothini

It can be done. Not a big issue, as both the objects are related.

 

But let me check. will let you know shortly.

iSqFt_ADiSqFt_AD

What needs to be added to the trigger then? Is there a resource I can see this as all versions I could find were only to clone the Opportunity and not with the Products.


Thanks!

Naidu PothiniNaidu Pothini

The process should be something like

 

1. Clone the Opportunity,

 

2. Insert the Cloned-Opportunity,

 

3. Clone the Products,

 

4. Insert the Cloned-Products  // This is where i am finding difficulties, We have to assigned the Opportunity Ids of the Cloned Opportunity to the products. I am trying to figure out how to do this with out any errors.

 

 

 

iSqFt_ADiSqFt_AD

I do not have the skill set to write this code though and what I have found floating around the internet references specific pricebooks, which I am not sure I want to do. Is there a way to simply clone the products from the original opportunity? What I found appears to always go to a specific pricebook.


Here is the link to the code I found - http://boards.developerforce.com/t5/Apex-Code-Development/How-to-insert-a-new-Opportunity-with-Products-in-a-trigger/m-p/162331/highlight/false#M24154