0

I am trying to create a custom controller and extension to create account records.

But I am stuck with this error I am facing. I have tried many other tips to make this work and but no avail.

This is the customer controller

 public class VFAccount {
    public account acc {
        get; //get accessor executes when the property is read
        set; //set accessor executes when the property is assigned a new value.
    }
     
    public VFAccount()
     {
       acc=new account();
     }
    
    //Creating records and displaying account details
     public PageReference create()
      { 
        try
        {
            insert acc;
        }
        catch(Exception e) {
            return null;
        }
          
        PageReference pref = new ApexPages.StandardController(acc).view();
        return pref;
    }
    
    //Cancel and displaying account list view
    public PageReference cancel(){
       PageReference pgref = new PageReference('/lightning/o/Account/list');
       return pgref;
    }
}

This is the extension of the custom controller

 public with sharing class ShowErroronVFPage {
    //public Account acc{
       // get;
        //set;
    //} 
    
    private final Account acc;
    
    public VFAccount controller {
        get; 
        set;
    }
    
    //Extension of StandardController.
    public ShowErroronVFPage(VFAccount controller){
       //controller.addFields(new List<String>{'Name', 'BillingState','Phone','Type','Industry','Rating'});
       //Account acc = (Account)controller.getRecord();
       this.acc = (Account)controller.getRecord();
    }
    
    public void create(){
        if(acc.Name == '' && acc.Name!= null){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Account name'));
        }
        if(acc.BillingState == '' && acc.BillingState != null){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Account Billing State'));
        }
        if(acc.Phone == '' && acc.Phone != null){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Account Phone'));
        }
        if(acc.Type == '' && acc.Type != null){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Account Type'));
        }
        if(acc.Industry == '' && acc.Industry != null){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Account Industry'));
        }
        if(acc.Rating == '' && acc.Rating != null){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Account Rating'));
        }
    }
}
}

I will be glad if anyone can help me with this issue. Any tips to this problem will be appreciated.

THANKS

1

An extension controller must have a constructor that receives an ApexPages.StandardController object (as it's bound to an object standard controller). In your case:

public ShowErroronVFPage(ApexPages.StandardController controller){
   this.acc = (Account)controller.getRecord();
}

Said that:

  1. I recommend having a more meaningful name for your standard controller class. If it's an Account one: AccountController, for instance.

  2. I understand you are creating a page that inserts account records. You don't need a custom controller for that. You can integrate what your VFAccount controller does into your extension controller, as you need the account fields info to be able to insert it, don't you?

4
  • I understand your statement. – Pranay Agarwal Jan 12 at 11:18
  • Yeah but, I need to insert account records and direct to the account details page and if cancel, then it directs to account list view. So,I created a custom controller for that. Extension is for checking the account details to see whether is it empty or not. is it is empty then it show errors – Pranay Agarwal Jan 12 at 11:33
  • You can do the same in the extension, just copy the methods! – Alba Rivas Jan 12 at 11:37
  • Yeah, I did that and It's working thanks! – Pranay Agarwal Jan 12 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.