0

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.

1

1 Answer 1

1

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) { }
}

Not the answer you're looking for? Browse other questions tagged .