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
ZAC BONNER 1ZAC BONNER 1 

Custom Button to update Lead Status to Unqualified

Hello,
I am trying to create a custom button for the Lead Page Layout to mark a lead as "Unqualified". My code is below; I get no syntax errors but I get the following error message when I click it on a Lead record:

" A problem with the OnClick JavaScript for this button or link was encountered:
Unexpected token catch"


My Code:

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

{
var url = parent.location.href; 
var updateRecords = []; 
var update_Lead = new sforce.SObject("Lead"); 
update_Lead.Id ="{!Lead.Id}"; 
update_Lead.Status="Unqualified";
updateRecords.push(update_Lead); 

result = sforce.connection.update(updateRecords); 
parent.location.href = url; 
} catch (e) {
alert (e);
}
Vishal_GuptaVishal_Gupta
Hi Zac,

Please use below code and let me know if you will receuve same error message again, you have missed try in your code :
 
{!REQUIRESCRIPT("/soap/ajax/23.0/connection.js")} 
try
{
var url = parent.location.href; 
var updateRecords = []; 
var update_Lead = new sforce.SObject("Lead"); 
update_Lead.Id ="{!Lead.Id}"; 
update_Lead.Status="Unqualified";
updateRecords.push(update_Lead); 

result = sforce.connection.update(updateRecords); 
parent.location.href = url; 
} catch (e) {
alert (e);
}

 
BM 5BM 5
Can you please try the below code.

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

try{ 
var leadstatusupdate = new sforce.SObject("Lead"); 
leadstatusupdate .ID = "{!Lead.Id}"; 
leadstatusupdate.Status = "Unqualified"; 
result = sforce.connection.update([leadstatusupdate]); 

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" + 

); 
}

Thanks,
BM