I have a custom button called "Take Case" on the Case object. The "Take Case" button is used when a specific case is assigned to a queue and a member within that queue presses the button to take ownership of the case. How can I throw an error that says "You cannot take this case because you are not part of the queue" if someone outside of the queue presses the "Take Case" button?

This is what I have so far:


var caseObj = new sforce.SObject("Case"); 
caseObj.Id = '{!Case.Id}'; 
caseObj.OwnerId = '{!$User.Id}'; 
caseObj.Status = 'Working';
caseObj.Assign_to_Queue__c = 'False'; 
var result = sforce.connection.update([caseObj]); 

if (result[0].success=='false') { 
} else { 
  • Why not just use the built-in "Accept" button that cases already have?
    – sfdcfox
    Jan 22 '17 at 17:27
  • What are you getting in this alert(result[0].errors.message);
    – Ashwani
    Jan 22 '17 at 18:07
  • I can't use the Accept button because it needs to be taken from the record itself ...
    – Paul
    Jan 22 '17 at 18:14
  • @Ashwani how do i check? i'm not very familiar with JS
    – Paul
    Jan 22 '17 at 18:15
  • @Paul There's an Accept button you can add to a case page layout. While you can use JS, there's no need to.
    – sfdcfox
    Jan 22 '17 at 19:47

You have to build the logic like this.

  1. Find Previous owner and Current Owner
  2. Verify if previous owner is a 'Queue'
  3. If point 2 satisfies then find the current user is part of that queue.
  4. If point 3 satisfies then allow update the ownership, otherwise throw the error message.
  5. Also, wise to check, if current and previous owner are not same.
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]);   
        alert("You cannot take this case because you are not part of the queue");
if(previousOwner == currentOwner) 
    alert("You are already the Case Owner ");   
  • Santanu - thank you for your input! I tried using this code into the button, however, no matter what user I use (whether or not the user is in the appropriate queue), I am getting the following error: A problem with the OnClick JavaScript for this button or link was encountered: invalid or unexpected token.
    – Paul
    Jan 22 '17 at 18:44
  • Im editing the answer specially query part, pls be patient, otherwise take this logic and tryout Jan 22 '17 at 18:45
  • didn't mean to rush you! i really appreciate it
    – Paul
    Jan 22 '17 at 18:46
  • Thank you Santanu! This button worked for me. In this code, how would I make it so that if the ownership change is successful, the status is set to the value of "Working"?
    – Paul
    Jan 22 '17 at 19:16
  • 1
    define ownershipChangeRequest.Status = 'Working'; after line no.6 Jan 22 '17 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.