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
Angel30Angel30 

how do you call controller class methods in another class

Hi,
I  have a controller class A which has two methods ,first method which fetches ContactID and 2nd  method fetches the Module Id.
I am creating a page whose controller class B will fetch values from Methods of Class A.
Question-1 : How to call methods of controller A in controller B.
Should we write this in the constructor of the new controller B or a separate method inside it to call the Controller class A methods.


Once i fetch the values , i need to create a record in a junction object which will associate contactId and Module Id(which is fetched from the methods). Here id the Module is 1,2 only then create record in junction object else not required.How shall we put this logic.

Kindly help as i am new to this.
 
Angel30Angel30
any help
Raj VakatiRaj Vakati
Case 1  : Lets Say You are using StandardController  and need to call from the Other helper class or apex class
  A controller extension is any Apex class containing a constructor that takes a single argument of type ApexPages.StandardController or CustomControllerName, where CustomControllerName is the name of a custom controller you want to extend. In the below code i am overrolading the constructor  to call the StandardController   extension class from other class


Extension 
public class myControllerExtension {

    private final Account acct;
    
     
    public myControllerExtension(ApexPages.StandardController stdController) {
        this.acct = (Account)stdController.getRecord();
    }

	public myControllerExtension(){
		
	}
	public void hello(){
		
	}
	
    public String getGreeting() {
        return 'Hello ' + acct.name + ' (' + acct.id + ')';
    }
}

Helper CLass

 
public class myControllerExtensionCaller{
	myControllerExtension ext = new myControllerExtension() ;
	ext.hello();
	
}

Case 2  : Lets Say You are using  Controller  and need to call from the Other helper class or apex class

 
public class myController {

    private final Account acct;
    
    

	public myController(){
		
	}
	public void hello(){
		
	}
	
    public String getGreeting() {
        return 'Hello ' + acct.name + ' (' + acct.id + ')';
    }
}

helper class
 
public class myControllerExtensionCaller{
	myControllerExtension ext = new myControllerExtension() ;
	ext.hello();
	
}

Note that you can use the code without overriding the constructor