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 

VF page error please help

Hi All,

I am trying to add a custom clone buon on Account. Here is my page:
 
<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);

        }  
        return null;
    }
               
    public PageReference save()
    {
        insert objAccount;
        
        return new PageReference('/'+objAccount.id);
    }
}

When i click on clone buton on account i get the VF page error:
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.

Could you please help. I am not sure what is wrong in the code.

Thanks
 
Best Answer chosen by Richa_Learning
Pankaj_GanwaniPankaj_Ganwani
Hi,

Just following up on my previous comment. Please use below mentioned autorun function definition for accomplish your intended functionality:
 
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;
    }

 

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi,

Instead of inserting the account record in separate save method you should insert the account in autorun method itself and set the return type as return new pagereference('/'+objAccount.Id);
R Z KhanR Z Khan
Hi Richa,

You visualfroce doesnt havy any logic in it. It only display that error message without any conditions. WHat is supposed to be displayed?
Richa_LearningRicha_Learning
what i am trying to achieve is. Click on account clone button and then new record is created with few fields copied.
Pankaj_GanwaniPankaj_Ganwani
Hi,

Just following up on my previous comment. Please use below mentioned autorun function definition for accomplish your intended functionality:
 
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;
    }

 
This was selected as the best answer
R Z KhanR Z Khan
Where is the clone button. is it in your VF page?
The following code should help
public with sharing class AccountController {
    public Account objAccount {get;set;}                
    public Boolean errorOccured {get; set;}

    private Account objNewAccount;
    Map<String, Schema.SObjectField> mapAccountFields;

    public AccountController(ApexPages.StandardController controller){
        objNewAccount = (Account)controller.getRecord();

    }

    public PageReference clone(){
        errorOccured = false;
        if(objNewAccount != null){        
            objAccount = objNewAccount.clone(false,true,false,false);
            insert objAccount;

            return new PageReference('/'+objAccount.Id);
        } 
        errorOccured = true;
        return null;
    }
}

and VFpage would be
<apex:page standardController="Account" extensions="AccountController">

  <apex:sectionHeader title="Auto-Running Apex Code"/>
  <apex:commandButton="{!clone}" rerender="error"/>
  <apex:outputPanel id="error" rendered="{!errorOccured}">
      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>

 
R Z KhanR Z Khan
If you use the standard controller oyu dont need to query for the record or check page parameters. Just use the standard functionality
Richa_LearningRicha_Learning
Thank you both for the help.

@R Z Khan Button was on the page layout. I was callin gthe code using custom button