Client Script that checks a variable has been field in if users tries to assign it to a group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello guys,
I have this client script that checks whether a Variable (LoV) is selected if the user tries to assign the inc to a specific assignment group.
Is there a way to add a check before the script runs, that will allow the reassignment when the field is not present at all?
Thank you and have a happy holiday!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Based on my understanding, In client scripts, you have two options to check if a field exists before validating it:
g_form.hasField(field name)– This is the cleanest and most readable method. It returns true if the field exists on the form, and false if it does not.
g_form.getControl(fieldName)– This method returns the HTML element for the specified field. If the field does not exist on the form, it returns null.
Using either method allows your script to skip validation when the field is not present, so older incidents can be reassigned without issues, while new incidents still enforce the required selection.
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
A better approach to this is a simple Client Script onChange of Assignment Group, to make the variable mandatory:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (newValue == '302f396287ff22506682653e8bbb3542') {
g_form.setMandatory('core_sub_category', true);
}
}
with a similar one onLoad for existing records with this assignment group. You can add a field message if you want to call attention to the variable value needing populated, but this uses the out of box functionality for mandatory fields, so submitting a record/update with the variable empty will not be possible, and the script will not cause an error if the field/variable is not found.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
try this
💡 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
a week ago
Thank you for marking my response as helpful.
💡 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
