Client Script that checks a variable has been field in if users tries to assign it to a group

Stergios Stergi
Giga Guru

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.

function onSubmit() {
    // Get the selected assignment group
    var group = g_form.getValue('assignment_group');

    // Required variable
    var coreSub = g_form.getValue('core_sub_category');

    // Core & Core Peripherals sys_id
    var CORE_GROUP = '302f396287ff22506682653e8bbb3542';

    // If assigned to Core group AND no subcategory selected
    if (group === CORE_GROUP && !coreSub) {

        g_form.showFieldMsg(
            'core_sub_category',
            'You must select a Core Sub Category before assigning this group.',
            'error'
        );

        return false;  // prevents the catalog item from submitting
    }

    return true;
}
 
 
The issue is, the script prevents assignment even for inc that don't have the Variable (Core Subcategory) present.
 
We have such cases because we had inc submitted BEFORE the introduction of the Core Subcategory Variable.

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!
4 REPLIES 4

KanteS
Giga Guru

Hi @Stergios Stergi 

Based on my understanding, In client scripts, you have two options to check if a field exists before validating it:

  1. 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.

  2.  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

Brad Bowman
Kilo Patron

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.

Ankur Bawiskar
Tera Patron

@Stergios Stergi 

try this

function onSubmit() {
    // Get the selected assignment group
    var group = g_form.getValue('assignment_group');

    // Core & Core Peripherals sys_id
    var CORE_GROUP = '302f396287ff22506682653e8bbb3542';

    // Only enforce the rule if:
    // 1. We are assigning to the Core group, AND
    // 2. The core_sub_category field exists on the form
    if (group === CORE_GROUP) {
        // Check if the field exists on the form
        var fieldControl = g_form.getControl('core_sub_category');

        // If field exists on the form, then validate it
        if (fieldControl) {
            var coreSub = g_form.getValue('core_sub_category');

            // If no subcategory is selected, show error and block submit
            if (!coreSub) {
                g_form.showFieldMsg(
                    'core_sub_category',
                    'You must select a Core Sub Category before assigning this group.',
                    'error'
                );
                return false; // Prevents the form from submitting
            }
        }
        // If field does not exist on the form, do nothing (allow submit)
    }

    return true; // Allow submit in all other cases
}

💡 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

@Stergios Stergi 

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! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader