Email URL with ampersand

e_wilber
Tera Guru

I can't for the life of me get a simple link to work in a mail script.

var prefix = "<a href='" + gs.getProperty('glide.servlet.uri');
var main = "/acs?id=public_survey&instance_id=" + current.sys_id;
var last = "'>Click here to take your survey</a>";

template.print(prefix + main + last);

This produces the url: https://instance.service-now.com/acs?id=public_survey&amp;instance_id=3fdb0cd61b3df3009830433fbd4bcb4e

The link doesn't work with &amp; in it. When I update the link to just &, the link works great.

I tried using var main = "/acs?id=public_survey%26instance_id=" + current.sys_id; but that doesn't work, either.

How do I force the ampersand to stay exactly as it is in my link?

 

6 REPLIES 6

Nitin Panchal
Tera Guru

Hi, 

 Try encodeURI() function to build your URI in mail script. 

https://www.w3schools.com/jsref/jsref_encodeuri.asp 

 

Thanks,

Nitin

Hi,

I tried

var prefix = "<a href='" + gs.getProperty('glide.servlet.uri');
var main = "/acs?id=public_survey&instance_id=" + current.sys_id;
var last = "'>Click here to take your survey</a>";
var fullUrl = encodeURI(prefix + main + last);
template.print(fullUrl);

And it resulted in the & becoming &amp; instead which makes the URL broken.

https:/instance.service-now.com//acs?id=public_survey&amp;instance_id=3fdb0cd61b3df3009830433fbd4bcb4e

Nitin Panchal
Tera Guru

Hi,

I tried your script without encodeURI in mailscript and used it in notification, it built URL with "&"

https://instanceName.service-now.com/acs?id=public_survey&instance_id=9c573169c611228700193229fff72400

 

Usage. 

find_real_file.png

 

Mail script:

find_real_file.png

Nicholas3
Tera Contributor

I have the same problem, when generating a link from a mail script that has an & ampersand ServiceNow automatically re-writes the url to be &amp; instead.

I also tried encodeURI and encodeURIComponent neither work.

Has anyone figured out a solution to this? At this point i've wasted so much time on this I could have built my own script to insert an email to the sys_email table that won't re-write my url's to have &amp; in them....