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
klocworkklocwork 

Unexpected Identifier error in JavaScript button

I am spinning my wheels on this and completely stumped.  When I try this code I get "A problem with the OnClick JavaScript for this button or link was encountered: Unexpected identifier".

 

What I am trying to do is update a custom object (Sales_Cycle__c) and the Lead record that it relates to (Sales_Cycle__c.Lead__c).

 

Any help would be greatly appreciated!

 

Thank you.

 

{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}
var CycleUpdate = new sforce.SObject("Sales_Cycle__c");
var l = SELECT Id, Status FROM Lead where Id =\'"+ CycleUpdate.Lead__c + "\' limit 1";
CycleUpdate.Id='{!Sales_Cycle__c.Id }';
CycleUpdate.Sales_Cycle_Status__c = 'No Further Interest';
CycleUpdate.Date_Sales_Cycle_Ended__c = new Date();
l.Status = 'No Further Interest';
update CycleUpdate;
update l;
window.location.reload();

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
In JS, you have to use sforce.connection (e.g. sforce.connection.query, sforce.connection.update). Also note that CycleUpdate.Lead__c would be blank, since it's just a new JavaScript memory object. Try this:

var cycle = new sforce.SObject("Sales_Cycle__c")
cycle.Id = '{!Sales_Cycle__c.Id}'
var lead = new sforce.SObject("Lead")
lead.Id = '{!Sales_Cycle__c.Lead__c}'
cycle.Sales_Cycle_Status__c = 'No Further Interest'
cycle.Date_Sales_Cycle_Ended__c = new Date()
lead.Status = 'No Further Interest'
var result = sforce.connection.update([cycle])
result = sforce.connection.update([lead])
window.location.reload()

Note that you really should check the results to see if the save was successful, this is only for illustrative purposes.

All Answers

sfdcfoxsfdcfox
In JS, you have to use sforce.connection (e.g. sforce.connection.query, sforce.connection.update). Also note that CycleUpdate.Lead__c would be blank, since it's just a new JavaScript memory object. Try this:

var cycle = new sforce.SObject("Sales_Cycle__c")
cycle.Id = '{!Sales_Cycle__c.Id}'
var lead = new sforce.SObject("Lead")
lead.Id = '{!Sales_Cycle__c.Lead__c}'
cycle.Sales_Cycle_Status__c = 'No Further Interest'
cycle.Date_Sales_Cycle_Ended__c = new Date()
lead.Status = 'No Further Interest'
var result = sforce.connection.update([cycle])
result = sforce.connection.update([lead])
window.location.reload()

Note that you really should check the results to see if the save was successful, this is only for illustrative purposes.
This was selected as the best answer
klocworkklocwork
Thanks for the help!

Many of your answers have got me out of jams in the past - and happy to have your help direct on this.