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
Nag Raj 32Nag Raj 32 

How to call a normal apex class from a custom button ?

Hi, 
    I want to call normal apex class, from a custom button in the account detail page.

Thanks in advance.
Raj.
Raj VakatiRaj Vakati
You can able to do it in two ways .. 

Option 1 :
Create visualforce  page with standard controller and extension as your code
Then create a button
Add the button to the page layout
 
Code
 
<apex:page standardController="Account" extensions="YOURCODE AS EXTENSION">
    
</apex:page>

Option 2 :
Create an apex class
Call it from the javascript button
global class MyClass
{
    webservice static void myMethod() // you can pass parameters
    { 
         // Do something
Call your code form here 
    }
}
 

JavaScript code

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

if({!AAA__c.Name}!=Null)
{
    sforce.apex.execute("MyClass","myMethod",{}"});
    alert("This is {!AAA__c.Name}");
}


 
 
Raj VakatiRaj Vakati
The AJAX toolkit includes built-in support for invoking Apex through anonymous blocks or public webservice methods.
To invoke Apex through anonymous blocks or public webservice methods, include the following lines in your AJAX code:


Refer this link for more info

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_and_ajax.htm