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
George AdamsGeorge Adams 

Trouble with JavaScript Button

I'm trying to create a custom button that will appear on every case. It will allow the current user to click it and take ownership of the case.

This is the code I'm using (which I basically copied from the button I have for this same purpose on the Contact):
 
{!requireScript("/soap/ajax/33.0/connection.js")} 

var case = new sforce.SObject("Cases"); 
case.id = '{!Case.Id}'; 
case.ownerId= '{!$User.Id}'; 
var result = sforce.connection.update([case]); 

if (result[0].success=='false') {
     alert(result[0].errors.message);
} else {
     location.reload(true);
}

When I click the button, I get the error: "Unexpected token case"

Anyone know what I'm doing wrong here?

Thanks!
Best Answer chosen by George Adams
venkat-Dvenkat-D
Couple of things,
1) Change variable name to non key word
2) try this  var newCase= new sforce.SObject("Case");

Refer to https://success.salesforce.com/answers?id=90630000000hRFsAAM 

All Answers

venkat-Dvenkat-D
Couple of things,
1) Change variable name to non key word
2) try this  var newCase= new sforce.SObject("Case");

Refer to https://success.salesforce.com/answers?id=90630000000hRFsAAM 
This was selected as the best answer
George AdamsGeorge Adams
Thank you! It looks like that was the issue (along with me calling the Case object "Cases").

Here's the code that works for anyone seeing this in the future:
 
{!requireScript("/soap/ajax/33.0/connection.js")} 

var caseVar = new sforce.SObject("Case"); 
caseVar.id = '{!Case.Id}'; 
caseVar.ownerId= '{!$User.Id}'; 
var result = sforce.connection.update([caseVar]); 

if (result[0].success=='false') {
     alert(result[0].errors.message);
} else {
     location.reload(true);
}