Variable Set fields in Classic Catalog not working(g_form.setDisplay() and g_form.setMandatory())
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago - last edited 2 hours ago
Hello Everyone,
I am facing an issue with controlling Variable Set visibility and mandatory fields dynamically using Client Scripts in the Classic Catalog backend view.
I have a Catalog Item with a Variable Set called "Shipping Details"
Internal Name: shipping_address_reclaim_asset
Variable Names: first_name, last_name, street_address, apt_street_other, city, state_province, zip_postal_code.
I have a Script Include that checks if the logged-in user's company has "u_shipping_required = true" in a custom policy table (sn_hamp_territory_procurement). • Based on the result, I want to: → SHOW Variable Set + Make fields mandatory (if shipping required = true) → HIDE Variable Set + Remove mandatory (if shipping required = false)
✅ Script Include is returning correct response (true/false) ✅ GlideAjax call is working correctly ✅ STEP 6 alert fires confirming response = 'true' ✅ Everything works perfectly in ESC Portal ✅ g_form.setDisplay() works on regular catalog variables.
❌ g_form.setDisplay('shipping_address_reclaim_asset', false) → Variable Set still visible for ALL users in backend ❌ g_form.setMandatory('first_name', true) → Fields not showing mandatory (*) indicator in backend.
Script Include:
Client Scripts:
// OnLoad function onLoad()--> UI Type as All
Backend: Fields are not mandatory and variable set showing for all users
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago - last edited an hour ago
don't use variable set name in script, but use individual variables within that variable set
also add some delay using timeout as Ajax might take some time to bring the value from script include
function onLoad() {
checkShippingRequired();
}
function checkShippingRequired() {
var reqUserId = g_form.getValue('requested_for');
var ga = new GlideAjax('sn_hamp.ScriptIncludeName'); // use correct script include name
ga.addParam('sysparm_name', 'checkShippingRequired');
ga.addParam('sysparm_user_id', reqUserId);
ga.getXMLAnswer(analyzeResponse);
}
function analyzeResponse(response) {
response = (response || '').toString().trim();
setTimeout(function () {
setShippingFields(response === 'true');
}, 300);
}
function setShippingFields(required) {
var mandatoryFields = [
'first_name',
'last_name',
'street_address',
'city',
'state_province',
'zip_postal_code'
];
var allFields = [
'first_name',
'last_name',
'street_address',
'apt_street_other',
'city',
'state_province',
'zip_postal_code'
];
for (var i = 0; i < allFields.length; i++) {
var field = allFields[i];
if (required) {
g_form.setDisplay(field, true);
if (mandatoryFields.indexOf(field) > -1) {
g_form.setMandatory(field, true);
}
} else {
g_form.setMandatory(field, false);
g_form.setDisplay(field, false);
}
}
}
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi @Ankur Bawiskar , thanks for the quick response.
I used the same script in onLoad Client script which didn't work in backend.
Could you please edit the script include name in your response as it is confidential.
Backend view URL contains: instance.service-now.com/com.glideapp.servicecatalog_cat_item_view.do
Thanks,
Sattar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
did you try the script I gave?
if those variables are mandatory 1st make them non-mandatory and then hide
any other UI policies are showing those variables within variable set
Remember UI policy runs at last so it might override your logic
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Hi @Ankur Bawiskar , yes i tried the same script in Onload Client script.
Which works in Portal but not in backend.
No those fields are not mandatory in the dictionary level, by using script only we are making mandatory.
Thanks,
Sattar