• blittler
  • NEWBIE
  • 0 Points
  • Member since 2008

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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.

Code:
/* This code allows the javascript to access the API and push data into the Org.*/
{!REQUIRESCRIPT("/soap/ajax/12.0/connection.js")};
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.");
    return;
} 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";
     newTaskArray.push(newTask);
  }
}
var callCompleted = false;  
try
{
   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);
         }
    }
    window.location.reload(true);
}

}

massCompleteTasks();

 
This code for markcomplete works okay:

Code:
* This code allows the javascript to access the API and push data into the Org.*/
{!REQUIRESCRIPT("/soap/ajax/12.0/connection.js")};
sforce.connection.session = "{!$Api.Session_ID}";

function updateTask( )
{
 try
    {
  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 ) {
   alert(result[0].errors.message);
   return;
  }    
  window.top.location.href=window.top.location.href;
 } 
 catch (e) {
  alert(e);
 } 
}

updateTask();