[SalesForce] Field Update on Button click Using JavaScript

My field is not getting updated for Owner Id on Case Object

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}

var caseObj = new sforce.SObject("Case"); 
caseObj.Id = '{!Case.Id}'; 
var orgId = '{!Case.Original_Case_Owner__c}'; 
caseObj.Status = 'In Progress'; 
caseObj.RecordType = 'L2';

var records = sforce.connection.query("SELECT Id from User Where Name = '{!Case.Original_Case_Owner__c}'");
var records1 = records.getArray('records');
var userId = ''; 
for(var i=0;i<records1.length;i++)
{ userId = records1[i].Id; } 
caseObj.OwnerId= userId ; 

var result = sforce.connection.update([caseObj]); 
window.location.reload();

Best Answer

Try this

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}

var caseObj = new sforce.SObject("Case"); 
caseObj.Id = '{!Case.Id}'; 

var caseRecords = sforce.connection.query("SELECT Id from User Where Name = '{!Case.Original_Case_Owner__c}'");

var records = caseRecords.getArray("records");

caseObj.OwnerId = records[0].Id;
sforce.connection.update([caseObj]);

window.location.reload();
Related Topic