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
ckempckemp 

Testing for a System Administrator with JavaScript

I have a custom button that I have created that will call a web service.  I want the button to only work for System Administrators, so I added this JavaScript to the button with an OnClick event:

Code:
if (typeof(sforce) != "undefined") {
result = sforce.apex.execute("MyNamespace/MyWebService", "action", {ids : idsToSend}); } else { alert("You do not have sufficient privileges to do this."); }

This works just fine when I'm running it in my Developer's sandbox as a System Administrator and gives me the alert message when I am not a sysadmin.  However, when I install this in production it gives me the "You do not have sufficient privileges to do this" message no matter what type of user I'm logged in with, including System Administrators.  Does anyone know what's going on?  Is there a different/better way to test whether the user is a sysadmin or not with JavaScript?
CaptainObviousCaptainObvious

Usually when I restrict code to System Administrators, I use the profile name:

Code:
var userProfile = "{!$Profile.Name}";
if (userProfile=="System Administrator") {
 do something ...
} else { 
 do something else ...
}

There are several other ways of restricting the use of buttons:

  • Create page layouts for system administrators, and place the the admin buttons only on that layout.
  • Use the HYPERLINK function in a formula, adding CONTAINS($Profile.Name,"System Administrator").