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
Mitchell McLaughlin 1Mitchell McLaughlin 1 

OnClick JavaScript Error

Hello - Here's my code for my Request Validation Button on a lead. 

I got this error 
User-added image
 
{!REQUIRESCRIPT("/soap/ajax/21.0/connection.js")} 

var leadAddress = '{!Lead.Street}'; 
var leadCity = '{!Lead.City}'; 
var leadState = '{!Lead.State}'; 
var leadCountry = '{!Lead.Country}'; 
var leadZip = '{!Lead.PostalCode}'; 

if(leadAddress == null || leadAddress == ''){ 
alert('Please enter a valid Street Address along with the City, State/Province, Zip/Postal Code and Country before clicking on this button.'); 
} 
else if(leadCity == null || leadCity == ''){ 
alert('Please enter a valid Street Address along with the City, State/Province, Zip/Postal Code and Country before clicking on this button.'); 
} 
else if(leadState == null || leadState == ''){ 
alert('Please enter a valid Street Address along with the City, State/Province, Zip/Postal Code and Country before clicking on this button.'); 
} 
else if(leadCountry == null || leadCountry == ''){ 
alert('Please enter a valid Street Address along with the City, State/Province, Zip/Postal Code and Country before clicking on this button.'); 
} 
else if(leadZip == null || leadZip == ''){ 
alert('Please enter a valid Street Address along with the City, State/Province, Zip/Postal Code and Country before clicking on this button.'); 
} 
else{ 
var urlStr = '/a0J/e?' 
+ 'CF00Nj00000091TUH={!Lead.FirstName &" "& Lead.LastName }' 
+ '&CF00Nj00000091TUH_lkid={!Lead.Id}' 
+ '&retURL=%2F{!Lead.Id}' 
+ '&saveURL=%2F{!Lead.Id}' 
+ '&RecordType=012j0000000pfbL' 
+ '&ent=01Ij0000001EVex' 
+ '&00Nj00000091TU6={!Lead.Company}' 
+ '&00Nj00000091TUQ=Pending'; 

window.open(urlStr,"_self") 
}


I found out that this error happens if the address is filled in as two lines, because it's a "long text" field maybe?
If the street field is filled out as "1008 W. 8th Avenue, Ste. E" It is okay. But as soon as it becomes two lines, like this
"1008 W. 8th Avenue
Ste. E" it throws this error. 

Can someone tell me what i can do about this?

Thanks!
 
Temoc MunozTemoc Munoz
Hi Mitchell.

You will need to replace '<br>' with '\n' in JavaScript.
 
var leadAddress = '{!Lead.Street}'.replace(/(?:\r\n|\r|\n)/g, '<br />');
Mitchell McLaughlin 1Mitchell McLaughlin 1
I still got the same error once i added another line to Lead.Street

Any other suggestions?
{!REQUIRESCRIPT("/soap/ajax/21.0/connection.js")} 

var leadAddress = '{!Lead.Street}'.replace(/(?:\r\n|\r|\n)/g, '<br />'); 
var leadCity = '{!Lead.City}'; 
var leadState = '{!Lead.State}'; 
var leadCountry = '{!Lead.Country}'; 
var leadZip = '{!Lead.PostalCode}'; 

if(leadAddress == null || leadAddress == ''){ 
alert('Please enter a valid Street Address along with the City, State/Province, Zip/Postal Code and Country before clicking on this button.'); 
} 
else if(leadCity == null || leadCity == ''){ 
alert('Please enter a valid Street Address along with the City, State/Province, Zip/Postal Code and Country before clicking on this button.'); 
} 
else if(leadState == null || leadState == ''){ 
alert('Please enter a valid Street Address along with the City, State/Province, Zip/Postal Code and Country before clicking on this button.'); 
} 
else if(leadCountry == null || leadCountry == ''){ 
alert('Please enter a valid Street Address along with the City, State/Province, Zip/Postal Code and Country before clicking on this button.'); 
} 
else if(leadZip == null || leadZip == ''){ 
alert('Please enter a valid Street Address along with the City, State/Province, Zip/Postal Code and Country before clicking on this button.'); 
} 
else{ 
var urlStr = '/a0J/e?' 
+ 'CF00Nj00000091TUH={!Lead.FirstName &" "& Lead.LastName }' 
+ '&CF00Nj00000091TUH_lkid={!Lead.Id}' 
+ '&retURL=%2F{!Lead.Id}' 
+ '&saveURL=%2F{!Lead.Id}' 
+ '&RecordType=012j0000000pfbL' 
+ '&ent=01Ij0000001EVex' 
+ '&00Nj00000091TU6={!Lead.Company}' 
+ '&00Nj00000091TUQ=Pending'; 

window.open(urlStr,"_self") 
}
User-added image
 
Temoc MunozTemoc Munoz
OK, I think you have hidden carriage returns in your code.

Can you try this:
 
alert('{!JSENCODE(Lead.Street)}');

If this works, just use JSENCODE then