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

Sforce is not defined

I get "Sforce is not defined" error message on the following code for mass completing tasks. The button is executing Javascript.

If the user has "View all Data" permission, I do not get the error message.

I read somwhere that AJAX libray is not being loaded and including {!REQUIRESCRIPT("/soap/ajax/12.0/connection.js")}; should fix it. But it doesn't.

Interesting, "mark complete" code for single task works okay - so i suspect there is an interaction with GETRECORDIDs.

/* This code allows the javascript to access the API and push data into the Org.*/
sforce.connection.session = "{!$Api.Session_ID}";

function massCompleteTasks( )
var taskArray = {!GETRECORDIDS( $ObjectType.Task )};
if (taskArray == null || taskArray.length == 0) {
    alert("Please select the tasks you wish to close.");
} else {
   var newTaskArray = new Array();
  for (var i = 0; i < taskArray.length; i++) {
     var newTask = new sforce.SObject("Task");
     newTask.Id = taskArray[i];
     newTask.Status = "Completed";
var callCompleted = false;  
   var result = sforce.connection.update(newTaskArray);
   callCompleted = true;
} catch(error) {
   alert("Failed to update Tasks with error: " + error);
if (callCompleted) {
    for (var i = 0; i < result.length; i++) {
         if (!result[i].getBoolean("success")) {
            alert("Task (id='" + newTaskArray[i] + "') could not be updated with error: " + result[i].errors);



This code for markcomplete works okay:

* This code allows the javascript to access the API and push data into the Org.*/
sforce.connection.session = "{!$Api.Session_ID}";

function updateTask( )
  var task = new sforce.SObject("Task");
  task.Id = "{!Task.Id}";
     task.Status = "Completed";
     var result = sforce.connection.update([task]);
     if (result[0].getBoolean("success") == false ) {
 catch (e) {



On what line did it give you that error?  The very first reference to sforce, or further in?
I don't get a line number.

The error pop says:

A problem with the onClick Javascript for this button or link was encountered:

sforce is not defined

Have you tried debugging that with Firebug?  The first question would be whether that REQUIRESCRIPT line is actually resolving itself to a reference to the AJAX Toolkit.  Depending on the context in which you're running this Scontrol, your merge fields may not be resolving themselves.

No I haven't debugged.

I need to figure out how to debug Javascript. I get the issue both in Firefox and IE.

Requirescript must be resolving itself since it works okay with the singe task complete (second piece of code)

Install the Firebug plugin for Firefox.  It makes debugging JavaScript a snap.
I'm not saying that REQUIRESCRIPT doesn't work in general -- I'm saying that perhaps the context of your first Scontrol is causing it not to resolve.
Thanks. I will install firefox plugin!

I am not sure what you mean by  "context of your first Scontrol is causing it not to resolve".

In both cases, I have defined a button with OnClick Javascript (technically no s-control).

The use case is slightly different - for "Mass Complete" one is selecting multiple tasks. For "Mark complete", it is the detail page of a task.

What would be the solution?

Message Edited by GoForceGo on 07-11-2008 04:32 PM
This problem seems to disappear if you disable "Enable Enhanced List" (feature from Summer 08). Unfortunately, I do need this feature.

Is this a bug Salesforce has introduced in Summer 08?

Without "Enable Enhanced List", I can find my javascript in the main script on the page.

With it turned on, I can't even see the definitions of buttons. I am no Javascript/DOM expert, so I don't know where they are hidden and where to find my OnClick Javascript.

But more importantly, I am wondering what the solution is!

Message Edited by GoForceGo on 07-11-2008 06:03 PM
Werewolf, seems like you responded. But I can't see your response. Not sure

I did not respond earlier.  I can't imagine why Enable Enhanced List would have any impact on the function of your Scontrol.  I still say run it through Firebug and examine the generated code to see what's up with it.
Are there any cheat sheets on how to debug using Firebug?

When I installed Firebug, there are a bunch of tabs on HTML, script.

Finding the OnClick Javascript code - I found it when I turn off "Enable Enhanced List". I can't find it when it is on.

When I did find it with "Enable Enhanced List" turnedd off, all the script is on one line. I can't set break points at each line.

I was hoping to set some breakpoints to what's going on.

Here's a quick tutorial:

Really you'll probably want to use the debugger keyword.  Just put a line somewhere in your code that says nothing but:


And the code will break to the debugger right there.
I too have this same issue.  I added a simple custom button which allows mass delete of leads from the list view, and am using enhanced lists.  When logged in as an admin (and therefore a user with View all Data permissions), it works; when logged in as a standard user it does not.  When I turned off enhanced lists, it works for any user.

If it is of any relevance, I am using the Professional edition with API enabled, so my ability to modify permissions outside of the default roles is pretty limited.

Code for this button was borrowed from a sample in the SF cookbook:

// Include and initialize the AJAX Toolkit javascript library 
// Get the list of accounts that should be deleted. 
// Use the $ObjectType merge field to indicate the type of 
// record Ids that are expected. 
var idsToDelete = {!GETRECORDIDS( $ObjectType.Lead)}; 
var deleteWarning = 'Are you sure you wish to delete ' + 
idsToDelete.length + ' leads—'; 
if (idsToDelete.length && (window.confirm(deleteWarning))) { 
// Delete the records, and pass a function into the call 
// so that the toolkit refreshes the current page 
// asynchronously when the call succeeds. 
function() {navigateToUrl(window.location.href);}); 
} else if (idsToDelete.length == 0) { 
alert("Please select the leads you wish to delete."); 


Message Edited by blittler on 07-27-2008 03:08 PM
I was having a similar problem, but for me it is while calling  executeanonymous from a Detail Page Button.

I found a fix on this forum page:

It appears that the "includes" for connection.js and apex.js are automatically placed on pages if you logon as a System Administrator (or via "logon as another user' as a Sys Admin), but not for "normal" users. Adding the REQUIRESCRIPT lines fixes this.

There's a better description of the problem I was experiencing in this blog post.

Message Edited by jrotenstein on 07-29-2008 11:43 AM
If you look at our codes, we (myself and blittler) both do have the REQUIRESCRIPT statement that includes connection.js.
I tried adding Apex.js and it doesn't change anything.

As I mentioned, the problem doesn't happen if you disable "Enable Enhanced Lists". So there is some connection there...

FYI - this issue will be fixed in a patch that is scheduled to be released this evening. Thanks!
Thanks! Sounds like it a known issue...I was about to open a case...


Hi, I am also getting the same error while clicking on a hover link of a List button. In general, if i click on the related list button, javascript is working fine. But, if i'm going throuh the hover link of that related list and clicking on the same button, this error is being displayed.


This problem is only for IE, and for one sandbox. ame code deployed in another sandboxes works fine.


Please help.



Thanks in advance,





continuing to the previous msg, i would like to put my script over here for your ref.




var accId = '{!Account.Id}';

var queryResult = sforce.connection.query("Select Id from Client_Brief__c where Account__c = '" + accId + "'");
var records = queryResult.getArray("records");
if(records.length != 0)
alert('Please use the existing Client Brief for this Account');

var clientBriefObjId = sforce.apex.execute("SuperViewConstant","getclientBriefObjId",{}); = "/"+clientBriefObjId+"/e?retURL=%2F{!Account.Id}&saveURL=/apex/ClientBriefSaveUrl?accid={!Account.Id}&nooverride=1";




For the normal related list button click, all alerts are being displayed. But for the hover link button click, only 'Hi1' is displayed and after that the error msg appears.

Is this issue resolved?



Actually this issue was appearing because of the feature "Enable separate loading of related lists" which was released with the latest editiion of salesforce. Disabling that feature solves the problem as of now, although that's not the proper way out of this.


Thanks & Regards,


Gagan BansalGagan Bansal

Add {!REQUIRESCRIPT("/support/console/26.0/integration.js")} in code if you are facing issue in Service Console.