g_form.setMandatory in client script is not working on the workspace but working in native view.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Monday
g_form.setMandatory in client script is not working on the workspace but working in normal view.
OnChange Client script
OnChange Field Name : Account
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
var gId = '1234493d47ba6a507c23de79116d7890';
if (newValue ===gId) {
g_form.setMandatory('service', false);
g_form.setMandatory('subcategory', false);
} else {
g_form.setMandatory('service', true);
g_form.setMandatory('subcategory', true);
}
return;
}
gId = '1234493d47ba6a507c23de79116d7890';
if (newValue ===gId) {
g_form.setMandatory('service', false);
g_form.setMandatory('subcategory', false);
} else {
g_form.setMandatory('service', true);
g_form.setMandatory('subcategory', true);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
I believe your requirement is simple
If account which user selects is that sysId (account) then make 2 fields non-mandatory and if it matches that account then make 2 fields mandatory
I assume you also want this when form loads and not only when Account changes
Please use this script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var gId = '1234493d47ba6a507c23de79116d7890';
var account = g_form.getValue('account');
if (account == gId) {
g_form.setMandatory('service', false);
g_form.setMandatory('subcategory', false);
} else {
g_form.setMandatory('service', true);
g_form.setMandatory('subcategory', true);
}
}
💡 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 || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
you can also use UI policy with this which runs onLoad as well
This will avoid scripting
💡 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 || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Monday
@Sathwik1 Check the UI Type field. It should be set to All or Workspace.
You can verify the fields you are manipulating "Service/subcategory" are visible in workspace view. Check the Isolate Script field on the Client Script form.
Tip: For simply setting fields mandatory or optional based on a condition, a UI Policy is generally the preferred, low-code method in ServiceNow and is more consistent across different UI types.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Monday
@Nilesh Pol shared screenshot above, please check..
