+ Start a Discussion
Btuitasi1Btuitasi1 

Custom button: ILLEGAL TOKEN error

I am trying to create a button to insert a text value into a field upon clicking. Here is my code:
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 

var newRecords = [];  

var idLob = new sforce.SObject(“Idea_Lobby__c”);  
idLob.id =”{!Idea_Lobby__c.Id}”;                  

idLob.Vote_Test__c =(’Up‘) ; 

newRecords.push(idLob);            

result = sforce.connection.update(newRecords); 
window.location.reload();

I am getting an illegal token error when I go to click it. Any ideas on how to fix this?

Thanks!
Best Answer chosen by Btuitasi1
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

It seems to be a problem with the single and double quotes and you the ('Up') should be 'Up' only:
 
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}

var newRecords = [];  

var idLob = new sforce.SObject("Idea_Lobby__c");  
idLob.id ="{!Idea_Lobby__c.Id}";                  

idLob.Vote_Test__c = "Up"; 

newRecords.push(idLob);            

result = sforce.connection.update(newRecords); 
window.location.reload();

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.

 

All Answers

Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

It seems to be a problem with the single and double quotes and you the ('Up') should be 'Up' only:
 
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}

var newRecords = [];  

var idLob = new sforce.SObject("Idea_Lobby__c");  
idLob.id ="{!Idea_Lobby__c.Id}";                  

idLob.Vote_Test__c = "Up"; 

newRecords.push(idLob);            

result = sforce.connection.update(newRecords); 
window.location.reload();

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.

 
This was selected as the best answer
Btuitasi1Btuitasi1
Thanks! Can I call this button from a visualforce page using <apex:commandbutton>?
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

I don't think this is possible but there's a way to get the same behavior:

1) Add the connection.js and a js var sessionId  to your VF:
<script type="text/javascript"> __sfdcSessionId = '{!$Api.Session_Id}'; </script>
<script src="/soap/ajax/29.0/connection.js" type="text/javascript"></script>

2) Create a js function enclosing the button's code:
 
function updateIdeaLobby (e) {
	e.preventDefault();
	
	var newRecords = [];  

	var idLob = new sforce.SObject("Idea_Lobby__c");  
	idLob.id ="{!Idea_Lobby__c.Id}";                  

	idLob.Vote_Test__c = "Up"; 

	newRecords.push(idLob);            

	result = sforce.connection.update(newRecords); 
	window.location.reload();	
}

3) Add to your commandButton an onclick event:
 
<apex:commandButton onclick="updateIdeaLobby(event)" />

That should do the work.

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
The js function must be surrounded by the <script type="text/javascript"> </script> tag as well.
Btuitasi1Btuitasi1
Okay, I've pasted in the code you provided. The page reloads, but the vote total remains the same. Maybe I am doing something wrong with the form? I only have the button within the form. Here is the code for that section:
<apex:form id="votingSection">
 <apex:pageBlock mode="edit"> 
<apex:pageBlockButtons location="bottom"> 
<apex:commandButton styleclass="btn-lg btn-data" value="Vote Up" onclick="updateIdeaLobby(event)"/> 
</apex:pageBlockButtons> 
</apex:pageBlock> 
</apex:form>

 
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Not sure as I don't have a holistic view of your scneario. What is the type of the field Vote_Test__c? Number or text? Because in your code you're just setting the text "Up" to your field, it's not working as a counter. If what you are looking for is that whenever the you hit the button, the number of votes gets increased you should consider changing your field Vote_Test__c to number and changing the following line as shown:
 
idLob.Vote_Test__c++;

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Btuitasi1Btuitasi1
It works! I made an error in the code you provided me. Though, I realize one could spam the vote button. Is there any logic to make clicks unique? Thanks for all the help so far!