function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mikhail Nitko 6Mikhail Nitko 6 

OnClick Javascript to Refresh page and then check conditions for possible Alert

Hi folks,

I put a custom button on Salesforce cases that both Accepts a case and checks whether it the case is owned by a User or Queue, and gives an error when one user tries to take the case from another User, and not from a Queue.

The trouble is this button only works if one user opens the case after another has accepted it.  But if both users open an unaccepted case, the button will unfortunately allow the case to first be taken by the first person and then the second, without alerting.

The code:
{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}



if('{!Case.OwnerId}'.substring(0,3)=='005'){
alert("This case is owned by " +'{!Case.Case_Owner_s__c}'+" and is not in a Queue.");
} else 

{

var caseObj = new sforce.SObject("Case");
caseObj.Id = '{!Case.Id}';
caseObj.OwnerId = '{!$User.Id}';
caseObj.One_Stop_Counter__c = "{!TEXT($User.Assigned_Counter__c)}";
caseObj.Display_Screen_Suffix__c = "see " + '{!User.FirstName}'+" at Counter  "+'{!TEXT($User.Assigned_Counter__c)}';
var result = sforce.connection.update([caseObj]);

if (result[0].success=='false'){
alert(result[0].errors.message);
} else {
window.parent.location.href="/{!Case.Id}";
}
}

My original solution to this was to have the code in the Accept button refresh the page before running its User owner check, but this does not seem to be working either.

I was adding location.reload(true); before the User check.

Can anyone tell me why the refresh solution isn't working, or if there is another way to either do the refresh, or to execute my intentions?
Best Answer chosen by Mikhail Nitko 6
LBKLBK
You need to check the Owner, onclick of the button, rather than fetching it from the page.

Try this code.
{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}
var objCase = sforce.connection.query("SELECT Id, OwnerId from Case where id ='{!Case.Id}' limit 1");
var records = objCase.getArray("records"); 
var caseOwner = records[0].OwnerId; 

if(caseOwner.substring(0,3)=='005'){
	alert("This case is owned by " +'{!Case.Case_Owner_s__c}'+" and is not in a Queue.");
} 
else 
{
	var caseObj = new sforce.SObject("Case");
	caseObj.Id = '{!Case.Id}';
	caseObj.OwnerId = '{!$User.Id}';
	caseObj.One_Stop_Counter__c = "{!TEXT($User.Assigned_Counter__c)}";
	caseObj.Display_Screen_Suffix__c = "see " + '{!User.FirstName}'+" at Counter  "+'{!TEXT($User.Assigned_Counter__c)}';
	var result = sforce.connection.update([caseObj]);

	if (result[0].success=='false'){
		alert(result[0].errors.message);
	} 
	else {
		window.parent.location.href="/{!Case.Id}";
	}
}
Let me know if this works for you.

All Answers

LBKLBK
You need to check the Owner, onclick of the button, rather than fetching it from the page.

Try this code.
{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}
var objCase = sforce.connection.query("SELECT Id, OwnerId from Case where id ='{!Case.Id}' limit 1");
var records = objCase.getArray("records"); 
var caseOwner = records[0].OwnerId; 

if(caseOwner.substring(0,3)=='005'){
	alert("This case is owned by " +'{!Case.Case_Owner_s__c}'+" and is not in a Queue.");
} 
else 
{
	var caseObj = new sforce.SObject("Case");
	caseObj.Id = '{!Case.Id}';
	caseObj.OwnerId = '{!$User.Id}';
	caseObj.One_Stop_Counter__c = "{!TEXT($User.Assigned_Counter__c)}";
	caseObj.Display_Screen_Suffix__c = "see " + '{!User.FirstName}'+" at Counter  "+'{!TEXT($User.Assigned_Counter__c)}';
	var result = sforce.connection.update([caseObj]);

	if (result[0].success=='false'){
		alert(result[0].errors.message);
	} 
	else {
		window.parent.location.href="/{!Case.Id}";
	}
}
Let me know if this works for you.
This was selected as the best answer
Mikhail Nitko 5Mikhail Nitko 5
Thank you LBK, this is fantastic.

The only thing I added was I had to change the Alert to retrieve and show the name of the owner from the live record, by adding this:
 
var caseOwnerName = records[0].Case_Owner_s__c;

if(caseOwner.substring(0,3)=='005'){
	alert("This case has already been accepted by " + caseOwnerName +" and is not in a Queue.");

Thank you tons.