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
- Go to Setup | Customize | Cases | Search Layouts | Buttons, Links, and Actions
 - Click 'New Button or Link' button
 - Select Display Type = List Button, and check Display Checkboxes (for Multi-Record Selection)
 - 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
- Go to Setup | Customize | Cases | Search Layouts | Cases List View
 - Click Edit link
 - Select button in Custom Buttons
 - Done and Save
 
Based on the solution above, you can use the same technique for other object with different criteria but similar purpose.
HI ,
ReplyDeleteCan 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.