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
GerhardNewmanGerhardNewman 

Call an Apex class from a button

I have an Apex class that I want to call from a button.  But I can't figure out how to call it from a button.
Note: I have tested this code inside a trigger and it works fine.
 
My button code is just (obviously this is wrong):
sforce.apex.execute("MyHelloWorld","doKPI", {});
 
The class code:
global class MyHelloWorld {

  WebService static void doKPI() {

    INTEGER NUM = 10;

    KPI__c[] tblkpi;

    tblkpi = new KPI__c[NUM];

    for (integer i = 0; i < NUM; i++) {
      tblkpi[i] = new KPI__c(UserName__c='user '+i, Signed_Contracts__c = i);
    }
    insert tblkpi;
  }
}
Best Answer chosen by Admin (Salesforce Developers) 
caroline_sfdccaroline_sfdc
Gerhard,

I think you might need to include the connection.js and apex.js files at the top of your button JavaScript:

Code:
{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}

If that doesn't fix your issue, you take a look at the Force.com Cookbook -- there's a recipe called "Creating a Button with Apex" on page 141 that shows a working example of a JavaScript button executing an Apex Web Service method -- perhaps there's something else in that example that will help you.

ADN members can download the book for free at http://wiki.apexdevnet.com/index.php/Members:Cookbook

Hope that helps!

Caroline Roth
Co-Author, "Force.com Cookbook"

All Answers

caroline_sfdccaroline_sfdc
Gerhard,

I think you might need to include the connection.js and apex.js files at the top of your button JavaScript:

Code:
{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}

If that doesn't fix your issue, you take a look at the Force.com Cookbook -- there's a recipe called "Creating a Button with Apex" on page 141 that shows a working example of a JavaScript button executing an Apex Web Service method -- perhaps there's something else in that example that will help you.

ADN members can download the book for free at http://wiki.apexdevnet.com/index.php/Members:Cookbook

Hope that helps!

Caroline Roth
Co-Author, "Force.com Cookbook"

This was selected as the best answer
GerhardNewmanGerhardNewman

Thanks very much Caroline - that worked!

I was working from the salesforce_apex_language_reference.pdf document yesterday and nowhere in that does it tell me the answer you just gave me.  I recommend that the document be updated to include that example so other beginners don't get stuck on such a trivial problem.

Thanks again!

caroline_sfdccaroline_sfdc
I'm glad that worked, Gerhard. I'll pass on your note to the Apex writer so that document gets updated ASAP!

Cheers,
Caroline
Chirag MehtaChirag Mehta
I too faced same issue. The buttons which were working fine all of a sudden started throwing "Sforce not defined" error. Post inclusion of those require scripts it started working.

Really Share n Dicuss among community members is Bottom Line of learning :)
NSmyrnosNSmyrnos

Does anyone know if this method should work in the Customer Portal?  I've had success with using this method on a normal SF user account, but keep getting the following error when executed from the CP:

 

A problem with the OnClick JavaScript for this button or link was encountered:

{faultcode:'sf:INSUFFICIENT_ACCESS', faultstring:'NOCPortalSubmitForReview: no access allowed to this class.', }

michael_j_ramsey@hotmail.commichael_j_ramsey@hotmail.com

Have you enabled the apex class for the customer portal profile in question?