Avatar

Mads Moi-Aune

Copy to Clipboard in Servicenow

Recently I was asked to find a way for users to easily create a string consisting of the task number and short description that they could paste into the time management software. In this short article we’re going to take a look at how we can copy something to your system clipboard from a UI Action in ServiceNow.

The snippet

if (navigator.clipboard.writeText) {
    var number = g_form.getValue('number');
    var short_description = g_form.getValue('short_description');
    navigator.clipboard.writeText(number + " - " + short_description).then(function() {
        console.log("Copied!");
    });
}

The if condition check wether your browser supports this function. Then we retrieve the value of fields number and short_description using g_form. Lastly we asynchronously write the the string to system clipboard. The navigator.clipboard.writeText is a native Javascript function and can be used on other sites aswell.