+ Start a Discussion
arasuarasu 

Why is my custom button javascript code not working in sandbox?

Hi,
 
I have a custom button that executes a javascript code when clicked. It works fine for all user profiles in production, but does not work in sandbox and throws the following error message:
 
[object Error]-sforce.apex is null or not an object
It works in sanbox only for administrator profile.

Appreciate is anyone can help identify the cause for this. This is my js code for the custom button:
 
Javascript Code for the custom button:
{!requireScript("/soap/ajax/11.1/connection.js")}
try{
var userid = '{!User.Id}';
var username='{!User.Name}'; 
var userEmail='{!User.Email}'; 
var apprId='{!Fixture_Order__c.Approver_Name__c}';
var fixOrder='{!Fixture_Order__c.Id }'; 
var vtoemailid='BHN.fixtures@bhnetwork.com';


var args = {puser:userid,pusername:username,puseremail:userEmail,pfixorderno:fixOrder,papprId:apprId,pemailtype:2,ptoEmailid:vtoemailid,pisruntest:false}; 
var result = sforce.apex.execute('HandlerFOSendEmail' , 'sendMail', args);

if(vCardKitsNeeded==1){
var args = {puser:userid,pusername:username,puseremail:userEmail,pfixorderno:fixOrder,papprId:apprId,pemailtype:3,ptoEmailid:vtoemailid,pisruntest:false}; 
var result = sforce.apex.execute('HandlerFOSendEmail' , 'sendMail', args);
}

if(vMerchReq==1){
var args = {puser:userid,pusername:username,puseremail:userEmail,pfixorderno:fixOrder,papprId:apprId,pemailtype:4,ptoEmailid:vtoemailid,pisruntest:false}; 
var result = sforce.apex.execute('HandlerFOSendEmail' , 'sendMail', args);
}

alert("The fixture order has been sent to BHN Fixtures.");

}catch(e)
 {
  alert("Send Email has failed. Please contact your system administrator with this screeshot or error message: "+e + "-" + e.message);
 }

 
mikefmikef
Make sure the 'HandlerFOSendEmail' class is added to all your user profiles in sandbox.

From help:

You can specify which users can execute methods in a particular top-level Apex class based on their profile. These permissions only apply to Apex class methods, such as Web service methods, or any method used in a custom Visualforce controller or controller extension applied to a Visualforce page. Triggers always fire on trigger events (such as insert or update), regardless of a user's permissions.

Note
If you have installed a managed package in your organization, you can set security only for the Apex classes in that package that are declared as global, or for classes that contain methods declared as webService.

If a user has the "Author Apex" permission enabled in his or her profile, the user has access to all Apex classes in the associated organization, regardless of the security setting for individual classes.

Permission for an Apex class is checked at the top level only. For example, if class A calls class B, and a user profile has access only to class A but not class B, the user can still successfully execute the code in class A. Likewise, if a Visualforce page uses a custom component with an associated controller, security is only checked for the controller associated with the page. The controller associated with the custom component executes regardless of permissions.

To set Apex class security from the class list page:
  1. Click Setup | Develop | Apex Classes.
  2. Next to the name of the class that you want to restrict, click Security.
  3. Select the profiles that you want to enable from the Available Profiles list and click Add.
  4. Select the profiles that you want to disable from the Enabled Profiles list and click Remove.
  5. Click Save.
To set Apex class security from the profile detail page:
  1. Click Setup | Manage Users | Profiles.
  2. Click the name of the profile you want to modify.
  3. In the Enabled Apex Class Access related list, click Edit.
  4. Select the Apex classes that you want to enable from the Available Apex Classes list and click Add.
  5. Select the Apex classes that you want to disable from the Enabled Apex Classes list and click Remove.
  6. Click Save.

arasuarasu

Hi Mike,

Thanks for your response. But I checked thoroughly and all access permissions for all classes and visualforce pages has been granted for all user profiles. I noted that it works only for System Administrator profile and not any other profile.
The behaviour is the same in both sanbox and production.

This is a critical issue for us now and greatly appreciate any help to identify the cause on why it throws the above error for all user profiles except system administrator.

Thanks and regards,
Ambili

mikefmikef
Sorry for the late response, is the HandlerFOSendEmail class global?

And/Or does the class reference any objects in salesforce that users don't have CRUD on?
kgrasukgrasu
Hi Mike,

Thanks for your response.  I added the following two lines of code at the beginning of the code and it worked!

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

Thanks and regards,
Ambili