I have already mentioned in my blog Take Ownership of Case from Case Details & ListView and step to steps how we can achieve this.
Taking ownership from Case Details Page
Create a Custom Button, with following characteristics:
- Display Type = "Detail Page Button"
- Behavior = "Execute Javascript"
- Content Source = "OnClick Javascript"
Use the following source code in the code section
Be sure to add the button the page layout.
{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")}
var caseObj = new sforce.SObject("Case");
var previousOwner = "{!Case.OwnerId}";
var currentOwner = "{!$User.Id}";
caseObj.Id = "{!Case.Id}";
caseObj.OwnerId = "{!$User.Id}";
caseObj.Status = 'Working';
//if previousOwner is queue
var ownerRec = sforce.connection.query("SELECT owner.type, ownerid, Id from Case WHERE owner.type= 'Queue' AND OwnerId='"+ previousOwner + "' AND Id='" + caseObj.Id + "'");
var records1 = ownerRec.getArray('records');
if(records1 !=null && records1.length>0)
{
//currentOwner is part of queue
var currentOwnerRec = sforce.connection.query("SELECT g.UserOrGroupId From GroupMember g WHERE groupId ='" + previousOwner + "' AND g.UserOrGroupId ='" + currentOwner + "'");
var records2 = currentOwnerRec.getArray('records');
if(records2 !=null && records2.length>0)
{
var result = sforce.connection.update([caseObj]);
location.reload();
}
else
{
alert("You cannot take this case because you are not part of the queue");
}
}
else
{
if(previousOwner == currentOwner)
{
alert("You are already the Case Owner ");
}
else
{
var result = sforce.connection.update([caseObj]);
location.reload();
}
}
Taking ownership of multiple cases from ListView.
The logic is still be same. User can choose multiple records and click on Take Ownership button in List View. If user is not part of any queues of the chosen records then system will show the alert "You cannot take this case because you are not part of the queue". Otherwise, system will allow user to take ownership at one go.
Create a Custom Button, with following characteristics
- Display Type = "List Button"
- Behavior = "Execute Javascript"
- Content Source = "OnClick Javascript"
Use the following source code in the code section
Be sure to add the button in Case Search Layout --> Cases List View.
{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")}
try {
var caseObj = new sforce.SObject("Case");
var selectedCases = {!GETRECORDIDS($ObjectType.Case)}; //chosen records from list view checkboxes
var caseIds = '';
var previousOwners = '';
var currentOwner = "{!$User.Id}";
var caseObjsForUpdate = [];
//check at-least one record is selected
if (selectedCases[0] == null) {
alert("You must select at least one record");
} else
{
//collect all the selected caseids in proper format
for (var i = 0; i < selectedCases.length; i++) {
caseIds = caseIds + ",'" + selectedCases[i] + "'";
}
caseIds = caseIds.substring(1, caseIds.length);
//if previousOwner is queue
var ownerRec = sforce.connection.query("SELECT owner.type, OwnerId, Id, Status from Case WHERE owner.type= 'Queue' AND Id IN (" + caseIds + ")");
var caseOwnerRecords = ownerRec.getArray('records');
if(caseOwnerRecords !=null && caseOwnerRecords.length>0)
{
var count =0;
//collect all the previous queues
for (var i = 0; i < caseOwnerRecords.length; i++) {
if(!previousOwners.includes(caseOwnerRecords[i].OwnerId))
{
count = count + 1;
previousOwners = previousOwners + ",'" + caseOwnerRecords[i].OwnerId + "'";
}
}
previousOwners = previousOwners.substring(1, previousOwners.length);
//currentOwner is part of all those queues
var currentOwnerRec = sforce.connection.query("SELECT g.UserOrGroupId From GroupMember g WHERE groupId IN(" + previousOwners + ") AND g.UserOrGroupId ='" + currentOwner + "'");
var records2 = currentOwnerRec.getArray('records');
//finally update all the records if count of queues selected and returning rows from query are same
if(records2 !=null && records2.length == count)
{
for (var i = 0; i < caseOwnerRecords.length; i++) {
caseObj = new sforce.SObject("case");
caseObj.Id = caseOwnerRecords[i].Id;
caseObj.OwnerId = "{!$User.Id}";
caseObj.Status = 'In Progress';
caseObjsForUpdate.push(caseObj);
}
var result = sforce.connection.update(caseObjsForUpdate);
if ( result[0].getBoolean( "success" ) ) {
location.reload( true ); // refresh page
} else {
var errors = result[0].errors;
var errorMessages = "";
for ( var i = 0; i < errors.length; i++ ) {
errorMessages += errors[i].message + '\n';
}
// display all validation errors
alert( errorMessages );
}
}
else
{
alert("You cannot take this case because you are not part of the queue");
}
}
}
} catch ( ex ) {
alert( ex ); // display any javascript exception message
}
Visualforce
page. – Adrian Larson♦ Feb 10 '17 at 22:59