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
Manish Anand 10Manish Anand 10 

Calling a method from controller extension to a custom controller.

How do I call a method from the controller extension to a custom controller?
Below is the controller extension class:-
public with sharing class ControllerEx1
{
   public final Account acct;
   public ControllerEx1(Apexpages.StandardController stdcon) {
   this.acct=(Account)stdcon.getRecord();
   }
   public Account returnAccount()
   { 
     
     return acct;
   }
     public PageReference Add_Con_Or_Opp()
     {
       PageReference pr = new PageReference ('/apex/ContactsOrOpportunity');
       pr.setRedirect(true);
       return pr;
     }
   }

I want to call returnAccount() from above CE to below controller method.
global without sharing class MyController2
{
   public Account acnt{get;set;}
  ControllerEx1 c=new ControllerEx1();
   acnt=c.returnAccount();
}
It throws an Compile Error: unexpected token: '='  at the last line
Best Answer chosen by Manish Anand 10
Mahesh DMahesh D
Hi Manish,

Please find the modified code:
 
public with sharing class ControllerEx1 {
    public Account acct;
    public ControllerEx1(Apexpages.StandardController stdcon) {
        this.acct = (Account)stdcon.getRecord();
    }
   
    public Account returnAccount() { 
        return acct;
    }
    
    public PageReference Add_Con_Or_Opp() {
        PageReference pr = new PageReference ('/apex/ContactsOrOpportunity');
        pr.setRedirect(true);
        return pr;
    }
}
 
public without sharing class MyController2 {
    public Account acnt{get;set;}
    
    public MyController2() {
    }
    public void getAccount() {
        Account acc = new Account();
        acc.Name = 'Test Account';
        insert acc;
        
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
        ControllerEx1 c = new ControllerEx1(sc);
        acnt = c.returnAccount();
    }
}

But you may need to change according to your requirement.

Please do let me know if it helps you.

Regards,
Mahesh

 

All Answers

pconpcon
That is because your code is not inside of any method.  You probably want to put it inside of the controller's constructor.
 
global without sharing class MyController2 {
    public Account acnt {
        get;
        set;
    }
    
    public MyController2() {
        ControllerEx1 c = new ControllerEx1();
        this.acnt = c.returnAccount();
    }   
}
NOTE: This code has not been tested and may contain typographical or logical errors
Mahesh DMahesh D
Hi Manish,

Please find the modified code:
 
public with sharing class ControllerEx1 {
    public Account acct;
    public ControllerEx1(Apexpages.StandardController stdcon) {
        this.acct = (Account)stdcon.getRecord();
    }
   
    public Account returnAccount() { 
        return acct;
    }
    
    public PageReference Add_Con_Or_Opp() {
        PageReference pr = new PageReference ('/apex/ContactsOrOpportunity');
        pr.setRedirect(true);
        return pr;
    }
}
 
public without sharing class MyController2 {
    public Account acnt{get;set;}
    
    public MyController2() {
    }
    public void getAccount() {
        Account acc = new Account();
        acc.Name = 'Test Account';
        insert acc;
        
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
        ControllerEx1 c = new ControllerEx1(sc);
        acnt = c.returnAccount();
    }
}

But you may need to change according to your requirement.

Please do let me know if it helps you.

Regards,
Mahesh

 
This was selected as the best answer