+ Start a Discussion
guenthmnguenthmn 

Mass Action Buttons for Tag and Search Results documentation?

I like to develop a Mass Action Button (new for the spring 08 release) to update multiple items in a related list, but can not turn up any documentation or some sample code.
 
Can someone point me in the right direction?
 
Thanks,
 
Matt
werewolfwerewolf
Here's an example (this is a custom button which mass accepts cases but prevents conflicts by not accepting cases that somebody else already accepted).  Note the !GETRECORDIDS bit -- that's the key.

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

var records = {!GETRECORDIDS($ObjectType.Case)};
var userId = "{!$User.Id}";
if (records[0] == null)
{
alert("Please select at least one case")
}
else
{
var query = "Select c.CaseNumber, c.Id, c.OwnerId, c.Owner.Name From Case c Where Id In (";
for (var n in records) {
query = query + "'"+records[n]+"'";
if (n!=records.length-1) {
query = query + ",";
}
}
query = query + ")";

var queryResults = sforce.connection.query(query);
var caseDetails = queryResults.getArray('records');

var updateRecords = [];
var casesAccepted = "";

var caseIter = new sforce.QueryResultIterator(queryResults);
while (caseIter.hasNext()) {
var caseObj = caseIter.next();

if (caseObj.OwnerId.substr(0,15) == userId)
{
alert("The case number "+caseObj.CaseNumber+" is already accepted by you.");
} else
{
if (caseObj.OwnerId.substr(0,3) == '005')
{
alert("The case number "+caseObj.CaseNumber+" is already accepted by "+caseObj.Owner.Name+".");
} else
{
var caseUpdate = new sforce.SObject("Case");
caseUpdate.Id = caseObj.Id;
caseUpdate.OwnerId = userId;
caseUpdate.Status = 'Assigned';
updateRecords.push(caseUpdate);
casesAccepted += caseObj.CaseNumber + " "
}
}
}

if (updateRecords.length>0) {
sforce.connection.update(updateRecords);
alert("The case numbers "+casesAccepted +"have been accepted by you.");
}
parent.window.location.reload();
}

 

guenthmnguenthmn

Thank you, that works very nice!

Sometime it's not quite obvious what's the best language/tool for the job.

Matt

steve_andersensteve_andersen
I've done the {!GETRECORDIDS()} trick a bunch in S-Controls, but I'm stumbling as I try to reimplement it in VisualForce. Can anyone help with how I get the list of Ids selected from a list button to a VF page?

Thanks,

Steve
werewolfwerewolf
I actually haven't tried it myself -- perhaps post that question to the Visualforce board and see what comes up?
Process KingProcess King
We are trying to create a "mass action" button that creates tasks for multiple contacts.  Simply put, we want to choose a lost of contacts and want to create the same task for each of them.  Can anyone provide sample code or additional hlpe on this subject?
 
Thanks!
werewolfwerewolf
Update: the code as posted breaks with enhanced lists.  Here is an updated version.

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

var records = {!GETRECORDIDS($ObjectType.Case)};
var userId = "{!$User.Id}";
if (records[0] == null)
{
 alert("Please select at least one case")
} else {
 //Get the cases that were selected and generate a query out of it
 var query = "Select c.CaseNumber, c.Id, c.OwnerId, c.Owner.Name From Case c Where Id In (";
 for (var n=0; n<records.length; n++) {
  query = query + "'"+records[n]+"'";
  if (n!=records.length-1) {
   query = query + ",";
  }
 }
 query = query + ")";

 var queryResults = sforce.connection.query(query);
 var caseDetails = queryResults.getArray('records');

 var updateRecords = [];
 var casesAccepted = "";
 var casesRejected = "";

 var caseIter = new sforce.QueryResultIterator(queryResults);

 //Iterate through the returned results
 while (caseIter.hasNext()) {
  var caseObj = caseIter.next();
  if (caseObj.OwnerId.substr(0,3) == '005')
  {
   casesRejected += caseObj.CaseNumber + " ";
  } else {
   var caseUpdate = new sforce.SObject("Case");
   caseUpdate.Id = caseObj.Id;
   caseUpdate.OwnerId = userId;
   caseUpdate.Status = 'Assigned';
   updateRecords.push(caseUpdate);
   casesAccepted += caseObj.CaseNumber + " "
  }
 }

 var alertText = "";

 if (updateRecords.length>0) {
  sforce.connection.update(updateRecords);
  alertText += "The case numbers "+casesAccepted +"have been accepted by you.";
 }

 if (casesRejected!="") {
  alertText += "These case numbers were already accepted by you or other users: "+casesRejected;
 }

 alert(alertText);

 parent.window.location.reload();
}