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
The Real Jonah HustThe Real Jonah Hust 

Custom button - check owner type prior to action (Java)

I  should start by saying I know very little about Java. I have pieced together a custom button that is used to update the open case record. The button works flawlessly, but I want to add code to check if its assigned to a user, and if it is, execute only the portion of the code that changes the priority. If its assigned to a queue, then the changes for Owner, Priority, and Status would need to execute. 

The code current code I have is:

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

var caseObj = new sforce.SObject("Case"); 
caseObj.Id = '{!Case.Id}'; 
caseObj.OwnerId = '00G36000001rInD'; 
caseObj.Status = "Ready to Move Forward"; 
caseObj.Priority = 'High' 
var result = sforce.connection.update([caseObj]); 
window.location.href=window.location.href;
Best Answer chosen by The Real Jonah Hust
swati_sehrawatswati_sehrawat
Hello Jonah,

Try this and let me know if it works.
 
{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")} 

var caseObj = new sforce.SObject("Case"); 
caseObj.Id = '{!Case.Id}';
var ownerId = '{!Case.OwnerId}';
if(ownerId.startsWith('00G')){
	caseObj.Priority = 'High'
} 
else if(ownerId.startsWith('005')){
	caseObj.OwnerId = '00G36000001rInD'; 
	caseObj.Status = "Ready to Move Forward"; 
	caseObj.Priority = 'High' 
}
var result = sforce.connection.update([caseObj]); 
window.location.href=window.location.href;

 

All Answers

swati_sehrawatswati_sehrawat
Hello Jonah,

Try this and let me know if it works.
 
{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")} 

var caseObj = new sforce.SObject("Case"); 
caseObj.Id = '{!Case.Id}';
var ownerId = '{!Case.OwnerId}';
if(ownerId.startsWith('00G')){
	caseObj.Priority = 'High'
} 
else if(ownerId.startsWith('005')){
	caseObj.OwnerId = '00G36000001rInD'; 
	caseObj.Status = "Ready to Move Forward"; 
	caseObj.Priority = 'High' 
}
var result = sforce.connection.update([caseObj]); 
window.location.href=window.location.href;

 
This was selected as the best answer
The Real Jonah HustThe Real Jonah Hust
It worked like a charm!

Thank you very much Swati. Greatly appreciated. 
swati_sehrawatswati_sehrawat
Great, Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution.