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
Lionel_CastroLionel_Castro 

Global method in visualforce pages

Hi All,

I've a method in a global class and I need that this method to be visible in all my visualforce pages... How I can do it?

Thanks
Best Answer chosen by Lionel_Castro
sandeep sankhlasandeep sankhla
Hi Juan,

Yes it is possible..

You can use one standardcontroller and then in extension you can all all your controllers which you want to use..

<apex:page standardcontroller="Account" extensions="controller1 controller2 controller3">


like I mentioned above you can sue space separted custom controllers...

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 

All Answers

sandeep sankhlasandeep sankhla
Hi Juan,

What do you mean by visible in VF page ?

You can simply use action function to call these methods from page..

Thanks,
Sandeep
Lionel_CastroLionel_Castro
Hi,

One VF page has got one controller, but if I got a global class and 2 VF pages, how I can call to global method in both pages? with action funcion? Can you put some example, please?

Thanks
sandeep sankhlasandeep sankhla
Hi Juan,

there is no difference in calling a global class method or public method from Vf page..
 
<apex:page controller="TestClass"> 
 <apex:form > 
  
  <apex:commandButton value="ssss" action="{!ssss}" immediate="false" rerender ="ss"/>
  <apex:outputpanel id="ss">
  </apex:outputpanel>
    </apex:form>
</apex:page> 



Class

global with sharing class TestClass {

public Contact objAcc{get;set;}
public String subjectInput{get;set;}


   public TestClass()
   {
   	
   }
   
   global void ssss()
   {
   	for(Account obj : [Select Id from Account where Id ='00128000002mclT'])
   	{
   		system.debug('==================='+obj.Id);
   		obj.Name='ss';
   		update obj;
   			system.debug('==================='+obj.Name);
   	}
   }

   
}

Please refer dummy code for example...

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
Lionel_CastroLionel_Castro
Sorry, I don't explained correctly.

My code will be:
 
Page 1

<apex:page controller="Controller1"> 
// call global property of TestClass
</apex:page> 

Page 2

<apex:page controller="Controller2"> 
// call global property of TestClass
</apex:page> 

Controller 1

public with sharing class Controller1{
}

Controller 2

public with sharing class Controller2{
}

Global Class

global with sharing class TestClass{

public String name {get;set;}

}

In this case the controllers can't extends other class...

Thanks again

 
sandeep sankhlasandeep sankhla
Hi Juan,

If you want to extend another class from your controller then you can use extends keyword tpo extend...the class which you are extending should be public abstract class className..

please check with thsi and let me know if you need any clarification....

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
Lionel_CastroLionel_Castro
Hi Sandeep,

I need fix this issue without extend...it's possible?

Thanks
sandeep sankhlasandeep sankhla
Hi Juan,

You mean you dont want to extend teh class from controller but you want to access them into page..which is something like you want to call more controllers from page instead of extending with class?

Thanks,
Sandeep
Lionel_CastroLionel_Castro
Exactly, I want to call one global method in all my VF pages, but I dont want to extend other class.

Thanks
sandeep sankhlasandeep sankhla
Hi Juan,

Yes it is possible..

You can use one standardcontroller and then in extension you can all all your controllers which you want to use..

<apex:page standardcontroller="Account" extensions="controller1 controller2 controller3">


like I mentioned above you can sue space separted custom controllers...

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
This was selected as the best answer
Lionel_CastroLionel_Castro
Thanks for you help!

 
sandeep sankhlasandeep sankhla
Hi Juan,

WC !! feel free to reach out for more doubts..

thanks,
sandeep