Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to hide a mandatory field using catalog client script?

The Matrix
Tera Contributor

Hi All,

I've written a onChange catalog client script on Subcategory field. whenever a specific subcategory is selected then the description field should be visible and should become mandatory. After Subcategory value changes back to --None-- the description field should be hidden (vice versa). I have written below script. It is not working. 

And we are adding category and subcategory choices via catalog client script. As these variables are from Variable set. How can I achieve this same functionality via catalog UI Policy. 

if (newValue == 'create_a_new_local_gcajob/sub_family') {
        g_form.setDisplay('description', true);
        g_form.setMandatory('description', true);
}
else 
{
        g_form.setMandatory('description', true); 
        g_form.setDisplay('description', true); 
}
        

 

10 REPLIES 10

Sarthak Kashyap
Mega Sage

Hi @The Matrix ,

I tried your problem in my PDI and it is working fine for me

Can you please try with UI Policy, 

 

SarthakKashyap_0-1761891086125.png

Create UI Policy Action 

SarthakKashyap_1-1761891114904.png

 

Result

When Option one is selected description is mandatory

SarthakKashyap_2-1761891152424.png

When Option two is selected description is not visible

SarthakKashyap_3-1761891177144.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak

 

 

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@The Matrix 

try this once -> do clear the description when you are hiding it

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

    if (newValue == 'create_a_new_local_gcajob/sub_family') { // use correct value to compare
        g_form.setDisplay('description', true);
        g_form.setMandatory('description', true);
    } else {
        g_form.setMandatory('description', false);
		g_form.clearValue('description');
        g_form.setDisplay('description', false);
    }

}

💡 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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@The Matrix 

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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ravi Gaurav
Giga Sage
Giga Sage

Hi @The Matrix 

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

// Target variable name: 'description'
if (newValue == 'create_a_new_local_gcajob/sub_family') {
g_form.setDisplay('description', true);
g_form.setMandatory('description', true);
} else {
g_form.setMandatory('description', false);
g_form.setDisplay('description', false);
g_form.clearValue('description');
}
}

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Ravi Gaurav
Giga Sage
Giga Sage

@The Matrix  if you feel like I am able to solve the problem .. Please accept the solution on the same !!

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/