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

Allen Andreas
Administrator
Administrator

Hello,

You are talking about adding a UI Action (button) to the form for this, but you haven't explained more about what the "copy text" use case is. Do you mean you want the text copied to then be placed somewhere else in the platform, such as a server script to take a field value and update another record on the platform with it?

Do you mean copy to the user's "clipboard" so that they can paste it elsewhere...like in another program/application?

Unfortunately, it's not very clear. Please give more information as to the entire picture.

 

Please mark reply as Helpful, if applicable. Thanks!


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

Exactly, I want the text to be copied into the user's clipboard so that the user can paste it anywhere he/she wants.

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!

Bert_c1
Kilo Patron

The following logic in a UI Action script worked for the original stated problem:

 

action.setRedirectURL(current);

var f1 = current.u_field_1;
current.u_field_2 = f1;
current.update();
gs.addInfoMessage('Field one was copied');

'Client' is not checked on the UI Action definition.

 

Coping to the "user's clipboard" seems challenging. But maybe someone will post how to do that.