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
pleclechpleclech 

How to call APEX code in a custom button

Hi,

 

here is the case :

 

I want to include a new custom button which call an APEX method that will create maybe more than 100 objects linked to the object we currently seeing.

As you can see I have some difficulties to do it with an APEX trigger because governor limits.

 

So the solution should be to create this objects by the user with a simple button on the layout.

 

Can you help ?

Best Answer chosen by Admin (Salesforce Developers) 
SFDCDev7SFDCDev7

Hi

 

To call a Apex class method from a button you need to define the method as "webservice".

 

You can call the method by using Ajax toolkit.

 

Below is the sample code.

 

Apex code:

 

global class class1{
WebService static Integer method1(String iTitle){

 

Your logic here

 

}

}

 

Custom button code:

 

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


var result = sforce.apex.execute("class1", "method1",{iTitle : noteTitle});

 

Hope it helps.

 

-AJ

All Answers

SFDCDev7SFDCDev7

Hi

 

To call a Apex class method from a button you need to define the method as "webservice".

 

You can call the method by using Ajax toolkit.

 

Below is the sample code.

 

Apex code:

 

global class class1{
WebService static Integer method1(String iTitle){

 

Your logic here

 

}

}

 

Custom button code:

 

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


var result = sforce.apex.execute("class1", "method1",{iTitle : noteTitle});

 

Hope it helps.

 

-AJ

This was selected as the best answer
pleclechpleclech

it sounds really good !

 

I wonder this kind of solution but now you confirm it's definitely the 1st solution that come in mind.

 

Thanks !

nrwingnrwing

Do you have any good references for documentation on this?  I could set this up using a trigger, but I would prefer not to if possible.  I assume just look for 'webservice' references, but if there is a specific source that talks on this, please post.....

 

Thanks,