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
SFDC_sadasSFDC_sadas 

Update Case Owner without Hardcoding User-ID

Hi All,
Using Custom button is it possible to update Case Owner without Hardcoding the User-ID.

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

var caseObj = new sforce.SObject("Case");
caseObj.id = '{!Case.Id}'; 
caseObj.Status = 'New'; 
caseObj.Complaint_Received__c= new Date();
caseObj.OwnerId = '00G26000000SsWo';
var result = sforce.connection.update([caseObj]);
if (result[0].success == 'false') {
alert(result[0].errors.message);
}
else {
location.reload(true);
}
 
Best Answer chosen by SFDC_sadas
KaranrajKaranraj
You can make query call in the Ajax javascript button and add the user Id based on your where condition in the query call. Try the below
{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")}
var contactCheck = sforce.connection.query("SELECT Id from User Limit 1");
var records = contactCheck.getArray("records");

var caseObj = new sforce.SObject("Case");
caseObj.id = '{!Case.Id}'; 
caseObj.Status = 'New'; 
caseObj.Complaint_Received__c= new Date();
caseObj.OwnerId = records[0].Id;
var result = sforce.connection.update([caseObj]);
if (result[0].success == 'false') {
alert(result[0].errors.message);
}
else {
location.reload(true);
}
 

 

All Answers

KaranrajKaranraj
You can make query call in the Ajax javascript button and add the user Id based on your where condition in the query call. Try the below
{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")}
var contactCheck = sforce.connection.query("SELECT Id from User Limit 1");
var records = contactCheck.getArray("records");

var caseObj = new sforce.SObject("Case");
caseObj.id = '{!Case.Id}'; 
caseObj.Status = 'New'; 
caseObj.Complaint_Received__c= new Date();
caseObj.OwnerId = records[0].Id;
var result = sforce.connection.update([caseObj]);
if (result[0].success == 'false') {
alert(result[0].errors.message);
}
else {
location.reload(true);
}
 

 
This was selected as the best answer
SFDC_sadasSFDC_sadas
Thanks, that did work!!