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
Lauren Hanna 2Lauren Hanna 2 

Javascript button on related list not working - unexpected or invalid token error

Hi All, 

I am having some issues with a list button that I want to create on the Opportunity object. Basically, I have a custom object that related to the standard opportunity object. My original goal was to query fields from the related Opportunity and Account object to auto-populate the custom "Escalations" object. 

I continue to get an error saying "invalid or unexpected token". At this point, I have continued to simplify the code to try and find the answer. I would be happy at this point, if someone could tell me the problem with the code below. (Which is much more simplified and doesn't even create anything, it just redirects the user to www.google.com). 
 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}

var OppDetail = sforce.connection.query("SELECT id,Name,AccountId,Account.Name
from Opportunity WHERE id = '{!Opportunity.Id}' LIMIT 1");


var records = OppDetail.getArray("records"); 

var OppName = records[0].name;


if(OppName==null){alert ("PROBLEM")} 


else { 

window.location="www.google.com";
}

Any help would be greatly appreciated. For info purposes, my original code was: 

{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}

var OppDetail = sforce.connection.query("SELECT id,Name,AccountId,Account.Name,
Expiring_Order_Total_del__c,Quote_Amount__c,
Account.Dont_Use_of_Customers_in_District_del__c,Account.Dont_Use_of_schools_in_disctrict_del__c from Opportunity WHERE id ='{!Opportunity.Id}' LIMIT 1");

var records = OppDetail.getArray("records"); 

var oppName = records[0].Name;
var oppid = records[0].id;
var AcctId = records[0].AccountId;
var AcctName = records[0].Account.Name;
var Ordertotal= records[0].Expiring_Order_Total_del__c;
var QuoteAmount= records[0].Quote_Amount__c;
var custindist = records[0].Account.Dont_Use_of_Customers_in_District_del__c;
var schoolsindist= records[0].Account.Dont_Use_of_schools_in_disctrict_del__c;


if(Name==null){alert ("PROBLEM")} 


else { 

window.location="/setup/ui/recordtypeselect.jsp?ent=01Ia00000026qDI&retURL=%2F"+Id+"& 
save_new_url=%2Fa0G%2Fe%3FCF00Na000000Arrzp="+name+
"&CF00Na000000Arrzp_lkid="+id+
"&CF00Na000000ArYAt="+AcctName+
"&CF00Na000000ArYAt_lkid="+AcctId+
"&00Na000000Ars4f="+custindist+
"&00Na000000ArYD9="+schoolsindist+
"&00Na000000ArYD4="+Ordertotal+
"&00Na000000Ars04="+QuoteAmount
}
Suraj PSuraj P
I think the "Invalid or Unexpected Token" error is due to a carriage return character within the query itself. I took it out and was able to successfully run the code you posted.
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}

var OppDetail = sforce.connection.query("SELECT id,Name,AccountId,Account.Name from Opportunity WHERE id = '{!Opportunity.Id}' LIMIT 1");


var records = OppDetail.getArray("records"); 
alert(JSON.stringify(records));

var OppName = records[0].Name;


if(OppName==null){alert ("PROBLEM")} 


else { 

window.location="https://google.com";
}

However, if you're trying to create a list button on the Opportunity object itself (and not the "Escalation" object related list on the Opportunity detail page), first of all, it would be a detail page button on the Opportunity, not a list button. Secondly, you can achieve the results using a URL button instead of a javascript button, since you can use merge fields in the url to get all the fields you're looking for. Somethin like:

/setup/ui/recordtypeselect.jsp?ent=01Ia00000026qDI&retURL=%2F{!Opportunity.Id}&save_new_url=%2Fa0G%2Fe%3FCF00Na000000Arrzp={!Opportunity.Id}&CF00Na000000Arrzp_lkid={!Opportunity.Id}&CF00Na000000ArYAt={!Account.Name}
&CF00Na000000ArYAt_lkid={!Account.Id}
&00Na000000Ars4f={!Account.Dont_Use_of_Customers_in_District_del__c}
&00Na000000ArYD9={!Account.Dont_Use_of_schools_in_disctrict_del__c}
&00Na000000ArYD4={!Opportunity.Expiring_Order_Total_del__c}
&00Na000000Ars04={!Opportunity.Quote_Amount__c}
 
samruddhi podesamruddhi pode
Suraj's answer is correct. It helped me.