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 

Email is triggered when i hit clone button

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 :

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

}
Your controller should be this!
 
Richa_LearningRicha_Learning
Deepak - I have logic on my controller that it should clone few field only..if i update controller as above it clones every field...I want to clone only those fields that are not in cloneaccount cusotm setings

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

}

 
Richa_LearningRicha_Learning
@Deepak - any suggestions?