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
Richa_LearningRicha_Learning 

Custom clone button on Account issue

Hi All,

I am creating a custom clone button on account. this is my VF page and controller. Button works fine but the issue is :
when i click clone the new record is created and saved.  I want when i click clone the page should remain in edit form and i should save it manually.

Please help.
 
<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>

Here is my controller:
public with sharing class AccountController {

    public Account objAccount {get;set;}        
    public string AccountID;                        
   
    private Account objNewAccount;
     Map<String, Schema.SObjectField> mapAccountFields;
    
    public AccountController(ApexPages.StandardController controller) 
    {
      
     }
               
              public PageReference autoRun()
 {
        AccountID = ApexPages.currentPage().getParameters().get('id');
                
        if(AccountID != null)

        {        mapAccountFields = Schema.SObjectType.Account.fields.getMap() ;

                objNewAccount = [ select id,OwnerId,AccountId,Status__c from Account where id = :AccountID ] ;

                objAccount = objNewAccount.Account(false,true,false,false);

             insert objaccount;

        return new PageReference('/'+objAccount.id);
    }
return null;
}

}

 
Best Answer chosen by Richa_Learning
Shikha AgashiShikha Agashi
You are actually inserting account here, that is creating account record. If you want to stay in edit page then you just need to write URL in pagereference.

All Answers

Shikha AgashiShikha Agashi
You are actually inserting account here, that is creating account record. If you want to stay in edit page then you just need to write URL in pagereference.
This was selected as the best answer
Richa_LearningRicha_Learning
Thanks It worked.....
Deepak GulianDeepak Gulian
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');
        clonePage.setRedirect(true);
        return clonePage;
    }   

}
It will redirect you to the clone page, before saving the new clone record!
 
Richa_LearningRicha_Learning
Thanks Deepak..but the issue what i am facing right now is..we have workflow rules that send an email as soon as record is created...when i hit clone the email is triggered...and when i hit save  the email goes out again.................I want email to go out only when i hit save.....could you please help
Deepak GulianDeepak Gulian
Use the controller extension code above which i provided, it will not insert record when you hit clone button, it will just redirect you to the edit page only, then hit Save . Then your record is inserted and email is triggered!
Richa_LearningRicha_Learning
Deepak i did update the controller as per your guidance.. but still email is triggering
 
public with sharing class AccountController {

    public Account objAccount {get;set;}        
    public string AccountID;                        
   
    private Account objNewAccount;
     Map<String, Schema.SObjectField> mapAccountFields;
    
    public AccountController(ApexPages.StandardController controller) 
    {
      
     }
               
              public PageReference autoRun()
 {
        AccountID = ApexPages.currentPage().getParameters().get('id');
                
        if(AccountID != null)

        {        mapAccountFields = Schema.SObjectType.Account.fields.getMap() ;

                objNewAccount = [ select id,OwnerId,AccountId,Status__c from Account where id = :AccountID ] ;

                objAccount = objNewAccount.Account(false,true,false,false);

             insert objaccount;

       PageReference cloneaccount = new PageReference('/'+accountid+'/e?clone=1');
        cloneaccount.setRedirect(true);
        return accountPage;
    }
return null;
}

}

 
Deepak GulianDeepak Gulian
The one you shared is an old controller mine is below, and in my controller I'm not inserting account just redirecting to edit page!
So use the below controller
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');
        clonePage.setRedirect(true);
        return clonePage;
    }   

}
This is new one!
 
Shikha AgashiShikha Agashi
Richa do not insert objaccount.
 
Richa_LearningRicha_Learning
Shikha :  i have logic on my controller that only clone few fields...If i remove insert it doesn;t work. and it gives me an error that "link is broken"

 
public with sharing class AccountController {

    public Account objAccount {get;set;}        
    public string AccountID;                        
      set<String> setExFields = new set<String>();
    private Account objNewAccount;
     Map<String, Schema.SObjectField> mapAccountFields;
    
    public AccountController(ApexPages.StandardController controller) 
    {
      
     }
               
              public PageReference autoRun()
 {
        AccountID = ApexPages.currentPage().getParameters().get('id');
                
        if(AccountID != null)

        {        mapAccountFields = Schema.SObjectType.Account.fields.getMap() ;
		
		
		
		List<CloneAccount__c > Exfields = Cloneaccount__c.getall().values();
                 
            for( Cloneaccount__c excludedField: ExFields){
                setExFields.add(excludedField.Name.toLowerCase());
            }
          
            for(String s: mapAccountFields.keyset()){
                if(!setExFields.contains(s)){
                    if(queryString == ''){
                        queryString += s;
                    }else{
                        queryString += ',' + s;
                    }
                }
            }

                objnewaccount = Database.Query('Select ' + queryString + ' From account where id= \'' + String.escapeSingleQuotes(accountID) + '\'');   

                objAccount = objNewAccount.clone(false,true,false,false);

          //   insert objaccount;

       PageReference cloneaccount = new PageReference('/'+objaccount.id+'/e?clone=1');
        cloneaccount.setRedirect(true);
        return accountPage;
    }
return null;
}

}