UI policy "Clear the variable value" makes a field editable in requested item view

Natalia4
Kilo Contributor

I've created a catalog item with several variables, then added UI policy to show and require some variables based on answer to a previous question.

  • "Applies on Requested Items" is checked on UI Policy
  • UI Policy Action is configured as follows: 

 find_real_file.png

It works fine in service portal, but then on requested item view the variable is editable (should be read-only) and the value is empty (it should include user's answer).

It works if I uncheck the 'Clear the variable value', but I want it to be cleared when user selects something else... Is there any other way to make it work?

 

 

8 REPLIES 8

Diogo Soares
Tera Expert

Hello.

 

Does your Catalog UI Policy run On load?

 

I think if the Catalog UI Policy run On load and Applies on Requested Items, if the Catalog UI Policy Action is set to true in Clear the variable value option, when the requested item loads the variable is already fulfilled and because of that the Catalog Conditions are evaluated returning true and so the Catalog UI Policy Action will be applied - meaning, will clear the variable on the requested item.

Noor Mohammad1
Kilo Guru

any solution for this I am facing the same issue

 

Anirudh_saraf
Tera Contributor

Any solution for this I am facing same issue

Hi @Anirudh_saraf @Noor Mohammad1 

Here's how you can implement the solution using Client Scripts in ServiceNow:

Client Script for Read-Only Behavior (onLoad function)

 

 

// Client Script to set read-only behavior based on conditions
function onLoad() {
    var answerToPreviousQuestion = g_form.getValue('previous_question_variable'); // Replace with actual variable name
    var fieldSysId = g_form.resolveNameMap('variable_name'); // Replace with actual variable name

    if (answerToPreviousQuestion === 'desired_value') {
        g_form.setReadOnly(fieldSysId, true);
    } else {
        g_form.setReadOnly(fieldSysId, false);
    }
}

 

 

 

Client Script for Value Clearing (onChange function)

 

 

// Client Script to clear variable value based on conditions
function onChange(control, oldValue, newValue, isLoading) {
    if (!isLoading) {
        if (newValue !== 'desired_value') {
            g_form.clearValue('variable_name'); // Replace with actual variable name
        }
    }
}

 

 

 

These Client Scripts provide granular control over field behaviors in ServiceNow, ensuring fields are correctly read-only or cleared based on user interactions.

--------------------------------------------------------------------------------------------------------------------
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.