I

‘ve answered several questions in the past about the functionality of lock icons in Service-now. Lock icons appear on glide_list and URL fields. The most-common example is the ‘Watch list’ field on the task table. Service-now provides some system properties to manage the default lock appearance of these field types. There are times when a global system property doesn’t really meet the need you have however, and you need to lock or unlock these fields automatically using script. In this article I’ll explain how I’ve done these types of things before.

GlideList-URL-Lock Fields

System Properties

First, the system properties. These can be found by navigating to ‘System Properties -> UI Properties’. Obviously they’re very simple to set, but they will apply system-wide, which limits their usefulness in many cases.

  • ‘Glide lists start out locked’ (glide.ui.glide_list.start.locked) – Controls the default lock behavior of ALL glide_list fields on form load
  • ‘Unlock empty URL fields on form load’ (glide.ui.unlock_empty_url) – Controls the default lock behavior of all empty URL fields on form load

Client Scripts

For more control over the lock/unlock behavior of these field types, you’ll need to use client scripts or UI policies. Here are some examples…

Lock/Unlock glide_list Fields

Lock glide_list field named ‘watch_list’ (Replace every instance of ‘watch_list’ with the name of your field and every instance of ‘incident’ with the name of the table the script runs against)

$('incident.watch_list_lock').click();

Unlock glide_list field named ‘watch_list’ (Replace every instance of ‘watch_list’ with the name of your field and every instance of ‘incident’ with the name of the table the script runs against)

$('incident.watch_list_unlock').click();

Lock/Unlock URL Fields

Lock URL field named ‘u_url’ (Replace every instance of ‘u_url’ with the name of your field and every instance of ‘incident’ with the name of the table the script runs against)

$('incident.u_url_lock').click();

Unlock URL field named ‘u_url’ (Replace every instance of ‘u_url’ with the name of your field and every instance of ‘incident’ with the name of the table the script runs against)

$('incident.u_url_unlock').click();

Advanced Custom Locking Scripts

Show/hide fields based on lock/unlock
This onLoad client script shows/hides the ‘short_description’ field when you click the lock/unlock icons on the ‘watch_list’ field. What you have to do to get this to work is define a custom event handler in your ‘onLoad’ client script. In this case, there is a built-in ‘onclick’ event that you need to access. You can see what the IDs and events are by using a DOM inspector like firebug in a Firefox browser. You can customize which field gets hidden by modifying the ‘g_form.setDisplay’ lines in the script below.

function onLoad() {
   //Get the control of the unlock icon
   var unlockControl = g_form.getControl('watch_list_unlock');
   //Set its onclick method
   unlockControl.onclick = unlockClick;
 
   //Get the control of the lock icon
   var lockControl = g_form.getControl('watch_list_lock');
   //Set its onclick method
   lockControl.onclick = lockClick;
}
 
function unlockClick() {
   g_form.setDisplay('short_description', false);
   unlock(this, 'incident.watch_list', 'incident.watch_list_edit', 'incident.watch_list.nonedit');
}
 
function lockClick(){
   g_form.setDisplay('short_description', true);
   lock(this, 'incident.watch_list', 'incident.watch_list_edit', 'incident.watch_list_nonedit', 'select_0incident.watch_list', 'incident.watch_list_nonedit');
}

Creating a custom lock icon for any form element
You can also add custom lock icons to other form elements. One example I’ve written in the past is a lock icon situated next to the ‘Category’ field on an incident form. Clicking the lock icon displays or hides other categorization fields. Check out the ‘Adding a UI Macro to a Non-reference Field’ article for more information.