How to hide a mandatory field using catalog client script?
						
					
					
				
			
		
	
			
	
	
	
	
	
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
Thursday
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); 
}
        
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
Thursday
Hi @The Matrix ,
please review below code-
- Catalog Client Script
- Applies to: the Catalog Item that includes your variable set
- Script Type: onChange
- Field name: subcategory
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) return;
    // Replace with your actual variable names
    var descVar = 'description';
    var subcat = newValue;
    // Example condition: when subcategory = 'hardware_issue'
    if (subcat === 'hardware_issue') {
        g_form.setDisplay(descVar, true);
        g_form.setMandatory(descVar, true);
    } else if (subcat === '') {
        // When set back to --None--
        g_form.setDisplay(descVar, false);
        g_form.setMandatory(descVar, false);
        g_form.clearValue(descVar);
    } else {
        g_form.setDisplay(descVar, false);
        g_form.setMandatory(descVar, false);
    }
}
If you found my response helpful, please mark it as helpful and accept it as the solution.
Thank you
Nawal Singh
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
Thursday
Hi @Nawal Singh Thanks for your help. Still no luck. It is not hiding mandatory fields
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
Thursday
Hi @The Matrix ,
When hiding a mandatory field in ServiceNow, you must first make it non-mandatory before hiding it.
// Make the field optional before hiding
if (newValue == "create_a_new_local....") {
g_form.setDisplay('description', true);
g_form.setMandatory('description', true);
} else {
g_form.setMandatory('description', false);
g_form.setDisplay('description', false);
}
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
Friday
Hi @The Matrix ,
Is your issue resolved or still facing the issue, let me know the issue so i will assist you on that!!
If you found my response helpful, please mark it as helpful and accept it as the solution.
Thank you
Nawal Singh
