I have a custom button on my Account
detail page called Change
. I need to call a public
method which is in a public with sharing class
. What I have learned is that we can access the method using sforce.apex.execute
if and only if the method is web service
of a global class
.
-
1You can declare the method as remote action and then call it from your page.developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/…– AtulRajguru9Commented Mar 4, 2016 at 5:33
1 Answer
You have already answered your own question. You can only call Apex
from a Javascript Button
if your class is global
and your method is webservice
. This question already has an answer here.
I can certainly understand a reluctance to go global
. Your alternative, if you would like to execute your Apex
methods while leaving them public
, is to instead make a Visualforce
button and call the methods from a controller extension.
Page
<apex:page standardController="Account" extensions="AccountChangeExtension"
action="{!executeApex}" />
Extension
public with sharing class AccountChangeExtension
{
final Account record;
final PageReference view;
public AccountChangeExtension(ApexPages.StandardController controller)
{
record = (Account)controller.getRecord();
view = controller.view();
}
public PageReference executeApex()
{
AccountServices.doSomething(record);
return view;
}
}
Service
public with sharing class AccountServices
{
public static void doSomething(Account record) { }
}