Catalog UI Policy with Rich Label in Employee Center is visible for invalid Single Line Text input

aparnaravi
Tera Contributor

Hi Team,

I am facing an issue with a Catalog UI Policy in Employee Center.

Requirement:

  • I have a Course Cost variable of type Single Line Text (there is no Integer/Decimal variable type available).
  • I have a Rich Label variable (retention policy) and a checkbox (retention consent).
  • The Rich Label and checkbox should be displayed only when:
    • Course Cost >= 10001
    • Requested For Location contains Asia

My Catalog UI Policy condition is:

  • course_cost >= 10001
  • Requested For Location contains Asia

The UI Policy has:

  • On Load = Checked
  • Reverse if False = Checked

The UI Policy Actions make both the Rich Label and checkbox Visible = True.

I also have:

  • An onSubmit Catalog Client Script that validates the Course Cost and prevents submission if the value is not numeric.
  • An onChange Catalog Client Script that calculates the retention period based on the Course Cost.

Issue:
If I enter an invalid value such as abc or hrdfbdfdh in the Course Cost field, the Employee Retention Policy (Rich Label) still becomes visible, even though the value is not numeric and the UI Policy condition should not evaluate as >= 10001.

Has anyone experienced this behaviour with Rich Label variables and Catalog UI Policies in Employee Center?

  • Is this a known limitation with Rich Label variables?
  • Is there a recommended way to control the visibility of a Rich Label based on a numeric value entered in a Single Line Text variable?

Any suggestions would be appreciated. Thank you!

 
 
 
 
1 ACCEPTED SOLUTION

Abhishek Pal
Giga Guru

Hey@aparnaravi 
This is not specifically a Rich Label limitation. The main issue is that Course Cost is a Single Line Text variable, so using a numeric condition such as course_cost >= 10001 in a Catalog UI Policy is not reliable when the value contains non-numeric characters.

I would not use the UI Policy condition for the numeric comparison. Instead, validate and control the visibility using an onChange Catalog Client Script.

For example:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading)
return;

var cost = (newValue || '').trim();
var location = g_form.getValue('requested_for_location') || '';

var validNumber = /^\d+(\.\d+)?$/.test(cost);
var showRetention = validNumber &&
                    parseFloat(cost) >= 10001 &&
                    location.toLowerCase().indexOf('asia') > -1;

g_form.setDisplay('retention_policy', showRetention);
g_form.setDisplay('retention_consent', showRetention);

if (!showRetention)
    g_form.setValue('retention_consent', 'false');

}

Replace requested_for_location, retention_policy, and retention_consent with the actual variable names.

If Requested For Location is a reference variable, getValue() returns the sys_id rather than the location name. In that case, use getReference() or populate the location into another variable and perform the check against that value.

I would also keep the existing onSubmit validation to prevent non-numeric Course Cost values from being submitted.

Also ensure both fields are hidden initially, either through a UI Policy or by calling setDisplay(false) onLoad.

So the recommended approach is:

Use the UI Policy only for simple visibility requirements.

Use a Catalog Client Script for the numeric comparison because Course Cost is stored as text.

Validate the value with a numeric regex before calling parseFloat().

Explicitly hide the Rich Label and checkbox when the value is invalid.

This should behave consistently in Employee Center and prevents values such as abc or hrdbfdfdh from satisfying the Course Cost condition.


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Abhishek Pal

View solution in original post

7 REPLIES 7

@aparnaravi 

Just following up to see whether my suggested solution worked for you.

If you're still facing any issues, I'd be happy to help further.

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

Tanushree Maiti
Tera Patron

Hi @aparnaravi 

 

Try this:

 

  1. Create a Catalog UI Policy with no conditions (or set it to run when Course Cost is not empty).
  2. Check the Run scripts checkbox.
  3. In the Execute if true block, add your evaluation:

 

function onCondition() {

    var cost = parseFloat(g_form.getValue('course_cost'));

    var location = g_form.getDisplayValue('requested_for_location');  // update your Actual variable

 

    if (!isNaN(cost) && cost >= 10001 && location.indexOf('Asia') > -1) {

        g_form.setDisplay('retention_policy', true);

        g_form.setDisplay('retention_consent', true);

    } else {

        g_form.setDisplay('retention_policy', false);

        g_form.setDisplay('retention_consent', false);

    }

}

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Abhishek Pal
Giga Guru

Hey@aparnaravi 
This is not specifically a Rich Label limitation. The main issue is that Course Cost is a Single Line Text variable, so using a numeric condition such as course_cost >= 10001 in a Catalog UI Policy is not reliable when the value contains non-numeric characters.

I would not use the UI Policy condition for the numeric comparison. Instead, validate and control the visibility using an onChange Catalog Client Script.

For example:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading)
return;

var cost = (newValue || '').trim();
var location = g_form.getValue('requested_for_location') || '';

var validNumber = /^\d+(\.\d+)?$/.test(cost);
var showRetention = validNumber &&
                    parseFloat(cost) >= 10001 &&
                    location.toLowerCase().indexOf('asia') > -1;

g_form.setDisplay('retention_policy', showRetention);
g_form.setDisplay('retention_consent', showRetention);

if (!showRetention)
    g_form.setValue('retention_consent', 'false');

}

Replace requested_for_location, retention_policy, and retention_consent with the actual variable names.

If Requested For Location is a reference variable, getValue() returns the sys_id rather than the location name. In that case, use getReference() or populate the location into another variable and perform the check against that value.

I would also keep the existing onSubmit validation to prevent non-numeric Course Cost values from being submitted.

Also ensure both fields are hidden initially, either through a UI Policy or by calling setDisplay(false) onLoad.

So the recommended approach is:

Use the UI Policy only for simple visibility requirements.

Use a Catalog Client Script for the numeric comparison because Course Cost is stored as text.

Validate the value with a numeric regex before calling parseFloat().

Explicitly hide the Rich Label and checkbox when the value is invalid.

This should behave consistently in Employee Center and prevents values such as abc or hrdbfdfdh from satisfying the Course Cost condition.


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Abhishek Pal