We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Dynamically remove reference qualifier onChange script

dougpenta
ServiceNow Employee

Is there a way to remove a reference qualifier acting on a Service Catalog variable when a user changes the value on another variable?

 Ex.

 

Field A (True/False)

Field B (Reference field)

 

if(Field A changes)

Field B (remove reference qualifier)

5 REPLIES 5

Ankur Bawiskar
Tera Patron

@dougpenta 

you can use advanced ref qualifier which depends on other variable.

using client script not possible to change reference qualifier

You are ServiceNow employee, you can check internally with the product team if you wish to.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@dougpenta 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Tanushree Maiti
Giga Sage

You can use an if statement as using the javascript: prefix enables that field to accept JavaScript.

If it's a bit more complex, then you can use a Script Include as well.

So use if statement , it's like:

 

javascript&colon; var queryVarB; if (current.variables.variable_A = = True) { queryVarB="<encodedQuery1>"; } else { queryVarB="encodedQuery2"; } queryVarB; 

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

Srikanth_9
Giga Guru
 
Yes, you can dynamically remove (or re‑apply) a reference qualifier on a Service Catalog variable when another variable changes, by using an onChange Catalog Client Script.
 
  • Field A → True/False (Checkbox)
  • Field B → Reference variable
  • Requirement:
    When Field A changes, remove the reference qualifier on Field B.
You do not remove the dictionary qualifier. You override it at runtime using g_form.setReferenceQualifier().
 

Create an onChange Catalog Client Script

  • Type: onChange
  • Variable name: field_a 
  • UI Type: All
  • Applies to: Catalog Item 

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

 

    if (newValue == 'true') {
        //Way to remove restriction
        g_form.setReferenceQualifier('field_b', 'sys_idISNOTEMPTY');
    } else {
        // Restore original qualifier
        g_form.setReferenceQualifier('field_b', 'active=true');
    }

 

    // Clear value to prevent invalid selections
    g_form.clearValue('field_b');

 

    // Required to re-evaluate the qualifier
    g_form.refreshReference('field_b');
}
 
 
If the provided solution is useful/working, please Accept as Solution and hit the Helpful. 
 
Thanks & Regards,
Srikanth Akula.