-1

I'm trying to conditionally clone a record. I'm using a trigger with this, but I just get a saved record with the values my trigger passes in. I've tried putting the page reference action in the apex button and nothing happens. What is going on here? I need the original copy preserved and the triggered copy for planning purposes.

Controller

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


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;

    //write some logic to check the future
    //if (opp.CreatedDate == newOpp.CreatedDate + 365 ){
    // return new PageReference('Opportunity already exists');}

     try{

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

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

}

This is the Apex Page

<apex:page standardController="Opportunity" extensions="FinancialCloningController" action="{!cloneOpportunity}" >                                                     
<apex:form >
<apex:pageBlock >
    <apex:pageBlockSection title="Planning">
        <apex:outputField value="{!Opportunity.Name }"  />
        <apex:outputField value="{!Opportunity.Intro_Date__c}"/>
        <apex:outputField value="{!Opportunity.Research_Date__c}"/>
        <apex:outputField value="{!Opportunity.Educate_Date__c}"/>
        <apex:outputField value="{!Opportunity.CloseDate}"/>
        <apex:outputField value="{!Opportunity.Ask_Ready_Date__c}"/>
        <apex:outputField value="{!Opportunity.Verbal_Date__c}"/>
        <apex:outputField value="{!Opportunity.Committed_Date__c}"/>
        <apex:outputField value="{!Opportunity.StageName}"/>


   </apex:pageBlockSection> 
   <apex:pageBlockButtons style="padding-left: 10%;">
        <apex:commandButton styleClass="btn" style="padding: 4px; width: 60px;" value="Save" action="{!Save}"/>
       <apex:commandButton styleClass="btn" style="padding: 4px; width: 50px;" value="Cancel" action="{!cancel}"/>
   </apex:pageBlockButtons>
</apex:pageBlock>

Here is the trigger:

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) {    
        if(x.Duplicated__c == false){
        x.Name = x.Name + ' planning';
        x.Duplicated__c = true; 
          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.StageName = '4 - Ask Ready';
                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.StageName = '4 - Ask Ready';
                x.Committed_Date__c = x.Committed_Date__c +365; 
                x.CloseDate = x.CloseDate + 365;

          }else if (x.StageName == '7 - Committed'){
                x.StageName = '4 - Ask Ready';
                x.Committed_Date__c = x.Committed_Date__c +365; 
                x.CloseDate = x.CloseDate + 365;

          }else{
                x.StageName = '4 - Ask Ready';
                x.Committed_Date__c = x.Committed_Date__c +365; 
                x.CloseDate = x.CloseDate + 365;
          }
          //get this picklist's value and update field with method. Has to be updated YEARLY.
          x.Designated_Fiscal_Year__c = x.Designated_Fiscal_Year__c ;

        if (oppList.size() > 0) insert oppList;
            oppList.add(x);
        }
    } 

Thanks.

1 Answer 1

0

Clone doesn't work because you specified all the parameters to clone as true. Thus, you're trying to insert to an existing record, which isn't allowed. A more typical scenario with clone is to use all false flags, instead:

     newOpp = opp.clone(false,false,false,false);

This clears out the Id, keeps shallow references, clears time stamps, and clears auto-number fields, respectively. This is usually the most ideal situation. Also, make sure you are querying all the fields you need, because whatever you don't query won't be set, so it will be missing data. Off the top of my head, you probably want to include at least AccountId and Amount, in addition to what you already have, as well as any important custom fields you have.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .