• Ajesh Pillai 2
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
I want to clone a record through Apex using a trigger to customize staging and update date fields.
I've run into two problems:

1. My original record isn't saved. I want the original record to save and a copy to be made from it. I'm looking for an automatic cloning action essentially.

The copy is known by the addage "planning" onto the name of the Opportunity.

2. The trigger is acting on all updates, meaning I can't change and save a field without activating the trigger. Do
I just need to change the parameters? Is there a way to check and limit with Trigger.Old?

Trigger that conditionally stages.
trigger FinancialPlanning on Opportunity (before update) {
 List<Opportunity> oppList = new List<Opportunity>();
    //creating list of opps in which items will live  
    for(Opportunity x : Trigger.new) {
          x.Name = x.Name + ' planning';
          
      
          if (x.StageName == '0 - Deferred'){
    			x.StageName = '1 - Research';
                x.Ask_Ready_Date__c = x.Ask_Ready_Date__c + 365;
                x.npsp__Ask_Date__c = x.npsp__Ask_Date__c + 365;
                x.Verbal_Date__c = x.Verbal_Date__c + 365; 
                x.Committed_Date__c = x.Committed_Date__c +365; 
                x.CloseDate = x.CloseDate + 365;
              
          }else if (x.StageName == '0 - Declined'){
                x.StageName = '1 - Research';
                x.Ask_Ready_Date__c = x.Ask_Ready_Date__c + 365;
                x.npsp__Ask_Date__c = x.npsp__Ask_Date__c + 365;
                x.Verbal_Date__c = x.Verbal_Date__c + 365; 
                x.Committed_Date__c = x.Committed_Date__c +365; 
                x.CloseDate = x.CloseDate + 365;
                
          }else if (x.StageName == '0 - Unresponsive') {
                x.StageName = '1 - Research';
                x.Ask_Ready_Date__c = x.Ask_Ready_Date__c + 365;
                x.npsp__Ask_Date__c = x.npsp__Ask_Date__c + 365;
                x.Verbal_Date__c = x.Verbal_Date__c + 365; 
                x.Committed_Date__c = x.Committed_Date__c +365; 
                x.CloseDate = x.CloseDate + 365;
               
          }else if (x.StageName == '1 - Research') {
				x.Ask_Ready_Date__c = x.Ask_Ready_Date__c + 365;
                x.npsp__Ask_Date__c = x.npsp__Ask_Date__c + 365;
                x.Verbal_Date__c = x.Verbal_Date__c + 365; 
                x.Committed_Date__c = x.Committed_Date__c +365; 
                x.CloseDate = x.CloseDate + 365;
               
          }else if (x.StageName == '2 - Intro'){
                x.Ask_Ready_Date__c = x.Ask_Ready_Date__c + 365;
                x.npsp__Ask_Date__c = x.npsp__Ask_Date__c + 365;
                x.Verbal_Date__c = x.Verbal_Date__c + 365; 
                x.Committed_Date__c = x.Committed_Date__c +365; 
                x.CloseDate = x.CloseDate + 365;
              
          }else if (x.StageName == '3 - Educate'){
                x.Ask_Ready_Date__c = x.Ask_Ready_Date__c + 365;
                x.npsp__Ask_Date__c = x.npsp__Ask_Date__c + 365;
                x.Verbal_Date__c = x.Verbal_Date__c + 365; 
                x.Committed_Date__c = x.Committed_Date__c +365; 
                x.CloseDate = x.CloseDate + 365;
              
          }else if (x.StageName == '4 - Ask Ready'){
                x.npsp__Ask_Date__c = x.npsp__Ask_Date__c + 365;
                x.Verbal_Date__c = x.Verbal_Date__c + 365; 
                x.Committed_Date__c = x.Committed_Date__c +365; 
                x.CloseDate = x.CloseDate + 365;
              
          }else if (x.StageName == '5 - Request'){
                x.Verbal_Date__c = x.Verbal_Date__c + 365; 
                x.Committed_Date__c = x.Committed_Date__c +365; 
                x.CloseDate = x.CloseDate + 365;
              
          }else if (x.StageName == '6 - Verbal'){
                x.Committed_Date__c = x.Committed_Date__c +365; 
                x.CloseDate = x.CloseDate + 365;
              
          }else if (x.StageName == '7 - Committed'){
                x.Committed_Date__c = x.Committed_Date__c +365; 
                x.CloseDate = x.CloseDate + 365;
              
          }else{
                x.Committed_Date__c = x.Committed_Date__c +365; 
                x.CloseDate = x.CloseDate + 365;
          }
          //get this picklist's value and update field with method. 
          x.Designated_Fiscal_Year__c = x.Designated_Fiscal_Year__c ; 
		  
        if (oppList.size() > 0) insert oppList;
            oppList.add(x);
      }
     
}
Here is what I've got in my controller class:
public class FinancialCloningController {
    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
    
    //instance for the variables being passed by id on the url
	private Opportunity opp {get;set;}
    
    //set the id of the record that is created
    public ID newRecordId {get;set;}
    
    public FinancialCloningController(ApexPages.StandardController controller) {
        this.opp = (Opportunity)controller.getRecord();
        this.controller = controller;
        opp = (Opportunity)controller.getRecord(); 
         
    }
    
    public PageReference cloneOpportunity() {

         // setup the save point for rollback
         Savepoint sp = Database.setSavepoint();
         Opportunity newOpp;

         try{

             //Copy the Opportunity - only include the records you want to clone 
             opp = [SELECT Id, Name, StageName FROM Opportunity WHERE ID = :opp.id];
             insert newOpp;
             newOpp.clone(false, false, false, false);

             //set the id of the new po created for testing
             newRecordId = newOpp.id;
             

         } catch (Exception e){
             // roll everything back in case of error
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
         }

          return new PageReference('/'+newOpp.id+'/e?retURL=%2F'+newOpp.id);
        
    }

}
I'm not sure that I can isolate a triggers actions to a button. Obviously, I'm very new to Salesforce and don't know if there is a way to do this without triggers. I feel the trigger is too powerful in this case. Why is the original record saving as a new record (conditional logic is fine and works), and can this trigger be isolated to a button action?

Thanks.