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
EldonEldon 

Javascript button to update a field

Hi,

Given below is my code for javascript button to update the status field
 
{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")} 

try{ 


var myquery = "SELECT Id, Name, status FROM lead WHERE Id = '{!Lead.Id}' limit 1"; 

result = sforce.connection.query(myquery); 
records = result.getArray("records"); 

var myLead= records[0]; 
var updateLead = new Array(); 

myLead.status = "Unqualified"; 
updateLead.push(myLead); 

result = sforce.connection.update(updateLead); 

if(result[0].success == "true"){ 
location.reload(); 
} 
else{ 
alert( 
"An Error has Occurred. Error: \r\n" + 
result[0].errors.message 
); 
} 
} 
catch(e){ 
alert( 
"An Un-expected Error has Occurred. Error: \r\n" + 
e 
); 
}

But i am geting the following error,

faultcode:'soapenv:Client', faultstring:'A duplicate value was specified for field 'Status' in object 'Lead', duplicate value 'Unqualified' prior value 'New'', }


What could be the reason?
Jarosław KołodziejczykJarosław Kołodziejczyk
Wouldn't this be easier to make using Lightning Component? And then create action to fire it.
Also this will be usable only in classic, as javascript buttons are not available in Lightning.

This is just my thought, no idea why this wouldn't work (the js code you provided), maybe it's due to non-restricted picklist allowing insert of new value and that making it duplicate, i can't test this so you have to see for yourself.
Ashish DevAshish Dev
Can you reply with console.log of your myLead javascript variable?