Quickstart: The Contention-Proof Case Accept Button

Available in: Salesforce Classic

Warning About This Page

NOTE: This technique is out of date. When using Omni-Channel, there is no contention.

This page was originally part of Developing with the Service Cloud.

Sample Code: The Contention-Proof Case Accept Button

This Javascript for a custom list button allows one to select a group of cases and will assign only those cases to the current user that another user hasn't already picked up.

var records = {!GETRECORDIDS($ObjectType.Case)};

var userId = "{!$User.Id}";

if (records[0] == null) {
	alert("Please select at least one case")
} else {
	//Get more info on the cases that were selected and generate a query out of it
	var queryResults = sforce.connection.retrieve("CaseNumber, Id, OwnerId,Owner.Type", "Case", records);
	var updateRecords = [];
	var casesAccepted = "";
	var casesRejected = "";
	//Iterate through the returned results
	for (var caseIndex=0;caseIndex<queryResults.length;caseIndex++) {
		var caseObj = queryResults[caseIndex];
		//If it's already owned by a user, add it to the reject pile
		if (caseObj.Owner.Type=='User') {
			casesRejected += caseObj.CaseNumber + " ";
		} else {
			//Otherwise accept it
			var caseUpdate = new sforce.SObject("Case");
			caseUpdate.Id = caseObj.Id;
			caseUpdate.OwnerId = userId;
			caseUpdate.Status = 'Assigned';
			casesAccepted += caseObj.CaseNumber + " ";
	//Give the user some feedback
	var alertText = "";
	if (updateRecords.length>0) {
		alertText += "You have accepted these cases:\n"+casesAccepted;

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

	//Reload the list view to show what he now owns