1

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:

{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")} 

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') { 
alert(result[0].errors.message); 
} else { 
location.reload(true); 
}
5
  • Why not just use the built-in "Accept" button that cases already have?
    – sfdcfox
    Commented Jan 22, 2017 at 17:27
  • What are you getting in this alert(result[0].errors.message);
    – Ashwani
    Commented Jan 22, 2017 at 18:07
  • I can't use the Accept button because it needs to be taken from the record itself ...
    – Paul
    Commented Jan 22, 2017 at 18:14
  • @Ashwani how do i check? i'm not very familiar with JS
    – Paul
    Commented Jan 22, 2017 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
    Commented Jan 22, 2017 at 19:47

1 Answer 1

3

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.
{!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");
    }
}
if(previousOwner == currentOwner) 
{   
    alert("You are already the Case Owner ");   
}
5
  • 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
    Commented Jan 22, 2017 at 18:44
  • Im editing the answer specially query part, pls be patient, otherwise take this logic and tryout Commented Jan 22, 2017 at 18:45
  • didn't mean to rush you! i really appreciate it
    – Paul
    Commented Jan 22, 2017 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
    Commented Jan 22, 2017 at 19:16
  • 1
    define ownershipChangeRequest.Status = 'Working'; after line no.6 Commented Jan 22, 2017 at 19:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .