How to add "Copy Text" feature on a form.

Hrishabh Kumar
Giga Guru

Requirement: I have a form with 2 fields "Input" and an "Output". I want to add a button on the form that says "Copy text" and it should have the functionality to copy whatever text is present in the output field. Output text should be copied in users clipboard so that he/she can paste it anywhere.

I am trying to use UI action scripts. How can I add the "copy text" feature?

 

copyText.PNG

1 ACCEPTED SOLUTION

Hi,

I'm glad my initial reply was Helpful to find out more information from you.

 

Ok, you can create a UI Action on this form, as a button, and client checkbox checked, and then use a script like in this thread to accomplish it: https://www.servicenow.com/community/developer-forum/how-to-copy-the-url-to-clipboard-using-client-s... 

So something like:

 

function copyString() {
var string = g_form.getValue('output_field_name');
copyToClipboard(string);
}

 

and place copyString() in the on-click field.

More example here as well: https://servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/ 

Create UI Action documentation: https://docs.servicenow.com/en-US/bundle/tokyo-platform-administration/page/administer/list-administ... 

 

Please mark reply as Helpful/Accept Solution, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

6 REPLIES 6

I appreciate your time, but this is not what I m looking for.

Rajesh Chopade1
Mega Sage

HI @Hrishabh Kumar 

 

Different browsers handle this differently and it can be a security issue, so I've used some client code like this in the past.

var field = g_form.getValue('fieldname');

window.prompt("Copy to clipboard: Ctrl+C, Enter", field);

 

It pops up a window with the text for the user to copy. You could stick that code in a client side ui action.

 

Thank you

Rajesh