AddThis Feed Button

Recent Items

Tags: Apex

Well, this little problem took my whole morning…

I wanted to give users a way to invoke my Webex Apex code, so I added a custom Button to my Campaign Page Layout.

I created a Detail Page Button that called the Apex:

sforce.apex.executeAnonymous('Process_Webex.process();')

This worked fine during all my testing, but when I moved it to Production, it generated an error:

Interestingly, it only happened in certain situations:

  • Logged in as System Administrator: Worked
  • Logged in as a Normal User: Did not work
  • Logged in as a Normal User via the System Administrator “login as another user” function: Worked

Further investigation of the HTML source code showed that <script> includes for connection.js and apex.js were being added to every Salesforce.com page only when logged in as a System Administrator (or “as another user”), but not as a Normal User.

I eventually found an article on the Community Forums titled “Call an Apex class from a button“, written by one of the authors of the Force.com Cookbook that recommends inserting the following code into the Button:

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

And that worked a charm!

Update 2008-07-30

Word has come through that a fix is being rolled-out (tonight!), so it was a real bug!

The Bottom Line

  • Seek and You Will Find (Translation: Google rox!)
  • Share and Enjoy (Translation: Sharing knowledge amongst the Community benefits all)
  • Even the best testing efforts can be thwarted unless you logon as the actual users, not just via the administrative Logon function

One Response to “Calling Apex from a Button – “Sforce is not defined””

  1. Guneet Says:

    Hi,

    Thanks for sharing this information. I had the same problem but in a slightly different avtaar.

    Had a couple of custom buttons (execute javascript) on some objects which were invoking Apex classes. The java script for these custom buttons was written a couple of months ago and was working fine.

    Incidentally, I did not have the any of the REQUIRESCRIPT statements in the java script but it was still working fine for both Administrators and other normal users. All these users were part of a custom profile.

    However sometime around last week it stopped working & started showing the error that you mentioned in your post (sforce is not defined). After spending some time, I figured out the problem and after adding the REQUIRESCRIPT statements, my custom buttons are working again.

    I’m not sure if there was more to that bug that you described or was a newer release strictly enforcing the REQUIRESCRIPT tag

    Thanks
    Guneet Sahai

Leave a Reply

*