Pages

Sunday, April 6, 2014

Salesforce: Selective Mass Close Case

This is continuation from blog Mass Close Case. Using out of the blog feature, user can close all Case selected, but how if user only allowed to close Case only with selected criteria?

You need to create new custom button using JavaScript (although this is also possible using Visualforce page with Apex code). Here we go:

Create custom button 
  1. Go to Setup | Customize | Cases | Search Layouts |  Buttons, Links, and Actions
  2. Click 'New Button or Link' button
  3. Select Display Type = List Button, and check Display Checkboxes (for Multi-Record Selection)
  4. Copy and paste script below:
 {!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}   
   
 var selectedCases = {!GETRECORDIDS($ObjectType.Case)};   
 if(selectedCases.length > 0){   
    var idList = "(";   
    for(var j=0; j<selectedCases.length; j++)   
    idList += "'" + selectedCases[j] + "',";   
    idList = idList.substring(0, idList.length - 1) + ")";   
   
    var searchResult = sforce.connection.query( "SELECT Id " +   
                                     "FROM Case " +   
                                     "WHERE " +   
                                     "( " +   
                                     "Status != 'New' AND " +   
                                     "Id IN " + idList +   
                                     " )"   
                                  );   
   
    if(searchResult.getInt("size") > 0){   
       var varCases = searchResult.getArray("records");   
       var varCasesToUpdate = [];   
       for(var i=0; i<varCases.length; i++){   
          var updatedCase = new sforce.SObject("Case");   
          updatedCase.Id = varCases[i].Id;   
          updatedCase.Status = 'Closed';   
          varCasesToUpdate.push(updatedCase);   
       }   
   
       var updateResult = sforce.connection.update(varCasesToUpdate);   
       location.reload();   
    }   
    else{   
       alert("Selected Case(s) are all Closed!");   
    }   
 }   
 else{   
    alert("Please select at-least one case." );   
 }  


Add button created to Cases List View
  1. Go to Setup | Customize | Cases | Search Layouts |  Cases List View
  2. Click Edit link
  3. Select button in Custom Buttons
  4. Done and Save

Based on the solution above, you can use the same technique for other object with different criteria but similar purpose.


1 comment:

  1. HI ,

    Can I achieve , adding selected records to related list. I have custom object A and B , they are look up to each other , hence added to related list . New button is there on related list but I have created custom button for adding existing value in section . Please help me with it. With help of vf page I am able to display all records and select unable to display in section.

    ReplyDelete

Page-level ad