How to copy the URL to clipboard using client script?

meetanand7
Tera Contributor

Hi,

I am trying to pass the URL to clipboard by using the attached code in client script, but it works in IE browser and not for Chrome. Could you please suggest what change I can do on this code for working in Chrome browser?

find_real_file.png

1 ACCEPTED SOLUTION

Domenic Horner
Tera Expert

Hi there,



I have tested the following code in IE 11, Chrome 62, and Firefox Quantum 57.



function onSubmit() {


      var instanceName = "something";


      var incidentURL = 'https://' + instanceName + '.service-now.com/incident.do?sys_id=' + g_form.getUniqueValue();


      copyToClipboard(incidentURL);


}



ServiceNow provide this "copyToClipboard" function that can be used, which will cause the "Copied to clipboard" message within servicenow.


find_real_file.png



You could also use the code located on this PasteBin to accomplish the same thing silently. function onSubmit() { var instanceName = "something"; var incidentURL - Pastebin.com



Setup in Client Script as follows:


find_real_file.png


View solution in original post

9 REPLIES 9

Domenic Horner
Tera Expert

Hi there,



I have tested the following code in IE 11, Chrome 62, and Firefox Quantum 57.



function onSubmit() {


      var instanceName = "something";


      var incidentURL = 'https://' + instanceName + '.service-now.com/incident.do?sys_id=' + g_form.getUniqueValue();


      copyToClipboard(incidentURL);


}



ServiceNow provide this "copyToClipboard" function that can be used, which will cause the "Copied to clipboard" message within servicenow.


find_real_file.png



You could also use the code located on this PasteBin to accomplish the same thing silently. function onSubmit() { var instanceName = "something"; var incidentURL - Pastebin.com



Setup in Client Script as follows:


find_real_file.png


Thanks for the help Domenic, it worked like a charm…..


HI,

 

This copyToClipboard() function is not working in Agent Workspace view. I can get the URL using "parent.location.href;" but cannot copy it to clipboard. Any workaround?

 

Thanks!

I know I'm a bit late, but this can be done using

 

navigator.clipboard.writeText("You just copied me");

Ankush6
Kilo Contributor

Hi team,

Any idea how to use this in Service portal ?