You need to sign in to do that
Don't have an account?
GerhardNewman
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; } }
I think you might need to include the connection.js and apex.js files at the top of your button JavaScript:
Code:
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
I think you might need to include the connection.js and apex.js files at the top of your button JavaScript:
Code:
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"
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!
Cheers,
Caroline
Really Share n Dicuss among community members is Bottom Line of learning :)
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.', }
Have you enabled the apex class for the customer portal profile in question?