0

I'm looking to replace an "Assign to Myself" button at the case object. I inherited an org and the initial button was created using Custom S-Control. This button creates an error when used with the omni-channel widget & service console.

I found code to update the button to use URL: /{!Case.Id}/a?retURL=%2F{!Case.Id}&newOwn_lkid={!$User.Id}&newOwn={!$User.FirstName} {!$User.LastName}&saveURL={!Case.Id}&save=1 --- however, even though this code auto populates the "self" user, the change case owner page shows and then still requires to click save.

I would like to be able to bypass this page and immediately change the ownership without confirming the change. I would then need to test to ensure an error isn't generated when using the omni-channel widget within the service console.

  • Seems like it would be easier to just write a Visualforce page. – Adrian Larson Feb 10 '17 at 22:59
  • Can also do this really easily using visual workflow. There is also the native "accept" button but I believe that is only on list views. – gorav Feb 11 '17 at 0:32
0

For now, you can use a custom JavaScript button, like so:

{!RequireScript("/soap/ajax/38.0/connection.js")}
var record = new sforce.SObject("Case");
record.Id = "{!Case.Id}";
record.OwnerId = "{!$User.Id}";
var result = sforce.connection.update([record]);
if(result[0].getBoolean("success")) {
   window.location.reload();
} else {
   alert(result[0].errors.message);
}

Note 1: Make sure the button's type is changed to "JavaScript".

Note 2: this won't work in Lightning Experience, so if you plan on using it, you'll eventually need to make up a component that does this.

Note 3: There's currently no confirmation at all, which is the point of the intermediate page requiring an extra click to confirm. You might still want to give the user the option.

  • Worked perfectly and worked perfectly in when using Omni-Channel widget/Service console. As for Note 3: This is identical to the functionality of the custom s-control coded button that users were use to. Regarding Note 2: I will keep that in mind. At this time all users are using classic. – Nyota Peek Feb 11 '17 at 3:03
0

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
}
  • Added Taking ownership from Case Details Page Script to button code and an error was received when code was added - A problem with the OnClick JavaScript for this button or link was encountered: Unexpected EOF – Nyota Peek Feb 11 '17 at 2:59
  • basically SOQL queries will be in single line. updated my code – Santanu Boral Feb 11 '17 at 3:36

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.