+ Start a Discussion
RedSalesRedSales 

Call Apex Class From A Custom Button.

Hello,

 

This is just a general question in relation to instantiating Apex classes from custom buttons.

 

I know a class which has a webservice defined method similar to the following can be instantiated from a custom button

 

global class MyClass {

  webservice static String processDetails(String accID){

 

     return 'test';

  }

}

 

 

The custom button has been created as a Detail Page Button executign Javascript.

 

The Javascript is as follows

 

 

{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")} 
try{ 
var accId='{!Account.Id}'
var result = sforce.apex.execute("MyClass", "processDetails",{accId : accId}); 
window.open( url); 
catch(err) { 
txt="There was an error on this page.\n\n"; 
txt+="Error description: " + err.description + "\n\n"; 
txt+="Click OK to continue.\n\n"; 
alert(txt); 
}

 

 

My question is. Do I have to declare my class methods as a webservice in order for this to work? I would have thought it should not have to be a webservice.

 

Thanks.

 

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

If you want to take the javascript invocation route, then yes, it does have to be a webservice method.

 

The other way to do it is via a VF Page and Custom Controller 

http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/

 

All Answers

CLKCLK

try it & let me know.

i also wanted to know same thing.

Ritesh AswaneyRitesh Aswaney

If you want to take the javascript invocation route, then yes, it does have to be a webservice method.

 

The other way to do it is via a VF Page and Custom Controller 

http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/

 

This was selected as the best answer