• RC Bommareddy
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
I've been researching this for two weeks, and I can only come up with half the solution.  I need to have the same capability as the standard Clone, but be able to pull in pre-populated fields.  We currently use Super Clone (https://appexchange.salesforce.com/listingDetail?listingId=a0N3000000B4mjFEAR), but I can't use that to pull data from one custom object (Expiring Structure) to another (Pricing Option).  Plus, with over a 100 fields on each page, it's really hard to maintain the page layouts.  

I'm starting with a simple clone of Pricing Option before I move onto something more difficult, but I'm lost as to how to proceed.  I DO NOT want to use a URL hack because that will not function correctly when we move to Lightning. I use that solution on several other objects, but I want to code this using Apex and VF on this object.

I've studied Jeff Douglas' code (http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/) and can successfully clone the record with pre-populated data.  Unfortunately, you have to insert the record and then open in edit mode.  I do not want the record created before the user chooses to complete the clone process.
//Created from instructions by Jeff Douglas: http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/

public with sharing class PricingOptionCloneController {
    
	//add an instance variable for the standard controller
    private ApexPages.StandardController controller {get; set;}

    //add the instance for the variables being passed by id on the url
    private Pricing_Option__c clonePO {get; set;}
    
    //set the id of the record that is created -- only used by test class
    public ID newRecordId {get; set;}
    
    //initialize the controller
    public PricingOptionCloneController(ApexPages.StandardController controller){
    	
    	//initialize the standard controller
        this.controller = controller;
        
        //load the current record
        clonePO = (Pricing_Option__c)controller.getRecord();
    
    }
    
    //method called from the VF's action attribute to clone the case
    public PageReference cloneSelectFields(){
    	
    	//setup the save point for rollback
    	Savepoint sp = Database.setSavepoint();
    	Pricing_Option__c newPO;
    	
    	try{
    		//copy the pricing option -- only include the fields that should be cloned
    		clonePO = [SELECT id, 
    					Opportunity__c,    					
    					Underlying_Comments__c
    				 FROM Pricing_Option__c
    				 WHERE id = :clonePO.id];
    		newPO = clonePO.clone(false);
    		newPO.Name = 'Autopopulated';
    		newPO.Pricing_Option_Description__c = 'update field';
    		newPO.Managing_Director_Pricing_Comments__c = 'update field';
    		insert newPO;
    		    		
    		//set the id of the new po created for testing
    		newRecordId = newPO.id;    		
    		    		
    	} catch (Exception e){
    		//roll the database back to before record was saved
    		Database.rollback(sp);
    		//add a page error/send an email/debug etc.
    		ApexPages.addMessages(e);
    		return null;
    	}
    	return new PageReference('/'+newPO.id+'/e?retURL=%2F'+newPO.id);
    }    
}

<apex:page standardController="Pricing_Option__c" 
    extensions="PricingOptionCloneController"
    action="{!cloneSelectFields}">
    <apex:pageMessages />
</apex:page>
I've also studied Deepak Gulian's code (https://developer.salesforce.com/forums?id=906F0000000D8XwIAK) and can successfully clone a record into edit mode without first inserting, but I cannot pre-prepopulate any of the fields.
//Created from instructions by Deepak Gulian: https://developer.salesforce.com/forums?id=906F0000000D8XwIAK

public with sharing class AccountController {

    public Account objAccount {get;set;}        
    public string AccountID;                        
   
    public AccountController(ApexPages.StandardController controller){       
       accountid = controller.getId();    
    }
   
    public PageReference autoRun(){
        PageReference clonePage = new PageReference('/'+accountid+'/e?clone=1&retURL=%2F'+accountid);
        clonePage.setRedirect(true);
        return clonePage;
    }   

}

<apex:page standardController="Account" extensions="AccountController" action="{!autoRun}">

  <apex:sectionHeader title="Auto-Running Apex Code"/>
  <apex:outputPanel >
      You tried calling Apex Code from a button.  If you see this page, something went wrong.  You should have been redirected back to the record you clicked the button from.
  </apex:outputPanel>
</apex:page>
I really need a blend of both techniques, but all of my attempts to modify their code has failed.  Can someone assist me in where I need to go?