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
Jason KlassJason Klass 

What's wrong with this code?

I'm trying to add a custom delete button to a view.  When I check the syntax below in the "custom button" page, it says there are no errors.  But when I try to use the button in the leads page, it says there is error in syntax.  Can someone tell me what's wrong?
 
Here's what I'm entering:
 
{!REQUIRESCRIPT("/soap/ajax/9.0/connection.js")}

var records = {!GETRECORDIDS( $ObjectType.Event )};
var taskRecords = {!GETRECORDIDS( $ObjectType.Task)};
records = records.concat(taskRecords);


if (records[0] == null) {
alert("Please select at least one record.") }
else {

var errors = [];
var result = sforce.connection.deleteIds(records);
if (result && result.length){
var numFailed = 0;
var numSucceeded = 0;
for (var i = 0; i < result.length; i++){
var res = result[i];
if (res && res.success == 'true'){
numSucceeded++;
} else {
var es = res.getArray("errors");
if (es.length > 0) {
errors.push(es[0].message);
}
numFailed++;
}
}
if (numFailed > 0){
alert("Failed: " + numFailed + "\nSucceeded: " + numSucceeded + " \n Due to: " + errors.join("\n"));
} else {
alert("Number of records deleted: " + numSucceeded);
}
}
window.location.reload();
}

}
if (numFailed > 0){
alert("Failed: " + numFailed + "\nSucceeded: " + numSucceeded + " \n Due to: " + errors.join("\n"));
} else {
alert("Number of records deleted: " + numSucceeded);
}
}
window.location.reload();
}
mojeebahmedmojeebahmed
Hi Jason,

    I have looked into the code and found out that
}

}
if (numFailed > 0){
alert("Failed: " + numFailed + "\nSucceeded: " + numSucceeded + " \n Due to: " + errors.join("\n"));
} else {
alert("Number of records deleted: " + numSucceeded);
}
}
window.location.reload();
}


This part of the code at the is redundent. try removing this and run the code. I have tried it and the code ran without any errors.

Code:
{!REQUIRESCRIPT("/soap/ajax/9.0/connection.js")}

 var records  = {!GETRECORDIDS( $ObjectType.Event )};
 var taskRecords = {!GETRECORDIDS( $ObjectType.Task)};
 records   = records.concat(taskRecords);


 if (records[0] == null) 
 {
  alert("Please select at least one record.") 
 }
 else 
 {
  var errors = [];
  var result = sforce.connection.deleteIds(records);

  if (result && result.length)
  {
   var numFailed = 0;
   var numSucceeded = 0;
   
   for (var i = 0; i < result.length; i++)
   {
    var res = result[i];
    if (res && res.success == 'true')
    {
     numSucceeded++;
    } 
    else 
    {
     var es = res.getArray("errors");
     if (es.length > 0) 
     {
      errors.push(es[0].message);
     }
     numFailed++;
    }
   }
   
   if (numFailed > 0)
   {
    alert("Failed: " + numFailed + "\nSucceeded: " + numSucceeded + " \n Due to: " + errors.join("\n"));
   } 
   else 
   {
    alert("Number of records deleted: " + numSucceeded);
   }
  }
  window.location.reload();
 }