We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

How to check variable visibility in Variable Editor (Backend) in a client script

Nick Parsons
Giga Sage

I have an "Email" variable on my RITM which looks like below:

NickParsons_0-1784782315721.png

Question:
Within a Client Script running on the RITM table (not a Catalog Client Script), how can I check whether this Email variable is currently visible in the Variable Editor, without relying on the specific UI Policy conditions behind its visibility?

The issue is that the g_form.isVisible() method does NOT check variable visibility in a client script like it does in a catalog client script. It only checks field visibility, so it returns false when the input is a variable.

 

Background:

I'm trying to make this Email variable mandatory when an RITM is moved to a closed state (Closed Complete, Skipped, or Incomplete), but only if the variable is currently visible.


I have a Client Script running on the RITM table (not a Catalog Client Script) that executes when the State field changes. When the state becomes closed, I want to make the Email variable mandatory only if it is visible, since making a hidden variable mandatory would cause it to become visible.


I tried using the script below, but g_form.isVisible() only checks fields on the RITM form itself and doesn't appear to work for variables in the Variable Editor. In this example, g_form.isVisible() is always false

function onChange(control, oldValue, newState, isLoading, isTemplate) {
    var CLOSED_COMPLETE = "3";
    var CLOSED_SKIPPED = "4";
    var CLOSED_INCOMPLETE = "7";

    var makeMandatory = newState == CLOSED_COMPLETE || newState == CLOSED_SKIPPED || newState == CLOSED_INCOMPLETE;
    if (g_form.isVisible("email")) {
        g_form.setMandatory("email", makeMandatory);
    }
}

 

Note: In this example, I could determine visibility by checking the checkbox value directly. However, I'm looking for a solution that doesn't rely on the underlying visibility conditions, since those conditions may change over time and require additional script maintenance.


What I've tried:

  • Using variables. prefix like so g_form.isVisible("variables.email")
  • Using the variable definition's ID g_form.isVisible("IO:b7bba9a093920f10c2a8f5532bba10c9")
  • Using isFieldVisible via g_form.isFieldVisible("email") along with the variables. and IO:<sys_id> options above
  • Accessing the variables control to check if it's visible via the DOM, however, g_form.getControl("email") does not yield any results (again tried with variables. and IO:<sys_id> options)

 

2 REPLIES 2

Nick Parsons
Giga Sage

I managed to find one solution (still open to others). It uses undocumented method g_form.getPrefixHandler() which allows you to access the g_form of the variable editor on Desktop (ie: Backend UI), and from there, check variable visibility:

function isVarVisible(fieldName) {
	try {
		var handler = g_form.getPrefixHandler(fieldName);
		if (handler) {
			return handler.getObject().isVisible(handler.getFieldName());
		}
		return false;
	} catch(e) {
		return g_form.isVisible(fieldName);
	}
}


To use it, use the variable name as is (no prefix or anything), eg:

if (isVarVisible("email")) {
  g_form.setMandatory("email", makeMandatory);
}


Of course, use this at your own risk, as undocumented methods may be subject to change and can be removed.

@Nick Parsons 

Thanks for sharing.

Please do mark your own response as correct so that it helps future members.

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader