Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Generate an Error Message in a Requested Item

vcaracci75
Tera Expert

I have created a requested item to create/modify a SharePoint site. If the request type field is Permissions Management, at lest one of the Level of Access fields needs to be filled out(see screenshot). If at lest one is not filled out, I need to generate an error message. I'm not quit sure how to do this. Any assistance would be greatly appreciated.

1 ACCEPTED SOLUTION

Hi @vcaracci75 ,

 

To show just one error message at the top of the form — instead of repeating it under each field — you can use g_form.addErrorMessage() instead of g_form.showFieldMsg().

 

Updated Script (Single Error Message at Top):

 

function onSubmit() {

    var requestType = g_form.getValue('request_type'); // Adjust to your actual field name

    var readOnly = g_form.getValue('read_only_groups_users');

    var contribute = g_form.getValue('contribute_groups_users');

    var other = g_form.getValue('other_please_note');

 

    if (requestType === 'Permissions Management') {

        if (!readOnly && !contribute && !other) {

            g_form.addErrorMessage('At least one access level must be specified.');

            return false; // Prevent submission

        }

    }

 

    return true; // Allow submission

}

What This Does:

  1. g_form.addErrorMessage() places a single error banner at the top of the form.
  2. It avoids cluttering each field with the same message.
  3. It’s ideal for validations that apply to a group of fields collectively.

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Best,

Anupam.

 

View solution in original post

5 REPLIES 5

Anupam1
Mega Guru

Hi @vcaracci75 ,

 

Your use case will be best handled with a Script Include + UI Policy or Client Script depending on whether you want the check to happen on the client side (form load/change) or server side (on submission).

Goal:

If Request Type is Permissions Management, then at least one of the following must be filled:

  • Read Only Groups/Users
  • Contribute Groups/Users
  • Other (please note)

If none are filled, show an error message and prevent submission.

 

 

Method 1:- Client Script (onSubmit)

Use a Client Script to validate before submission:

(function executeRule(current, gForm, gUser, gSNC) {

    var requestType = gForm.getValue('u_request_type'); // adjust field name

    var readOnly = gForm.getValue('u_read_only');       // adjust field name

    var contribute = gForm.getValue('u_contribute');    // adjust field name

    var other = gForm.getValue('u_other');              // adjust field name

 

    if (requestType === 'Permissions Management') {

        if (!readOnly && !contribute && !other) {

            gForm.showFieldMsg('u_read_only', 'At least one access level must be specified.', 'error');

            gForm.showFieldMsg('u_contribute', 'At least one access level must be specified.', 'error');

            gForm.showFieldMsg('u_other', 'At least one access level must be specified.', 'error');

            return false; // Prevent submission

        }

    }

    return true;

})(current, gForm, gUser, gSNC);

 

Note:- Adjust field names (u_read_only, u_contribute, etc.) to match your actual field names.

 

Method 2: UI Policy + UI Policy Script:

 

If you want to show/hide or enable/disable fields based on Request Type, use a UI Policy with a UI Policy Condition like:

u_request_type == 'Permissions Management'

 

Then add a UI Policy Script to validate:

if (!gForm.getValue('u_read_only') && !gForm.getValue('u_contribute') && !gForm.getValue('u_other')) {

    gForm.showFieldMsg('u_read_only', 'Please specify at least one access level.', 'error');

}

 

 

Optional Method: Server-side Validation

If you want to enforce this rule even if someone bypasses the client (e.g., via API), use a Business Rule (before insert/update) with similar logic.

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Best,

Anupam.

 

Hi Anupam,

Below is the client script I applied based on your suggestion. It didn't quite work though. What happened was the scrip disables all of my UI policies, and no error was generated when I left all three fields blank.

 

function onSubmit() {

    var requestType = gForm.getValue('read_only_groups_users');

    var readOnly = gForm.getValue('contribute_groups_users');      

    var contribute = gForm.getValue('contribute_groups_users');    

    var other = gForm.getValue('other_please_note');            

 

    if (requestType === 'Permissions Management') {

        if (!readOnly && !contribute && !other) {

            gForm.showFieldMsg('read_only_groups_users', 'At least one access level must be specified.', 'error');

            gForm.showFieldMsg('contribute_groups_users', 'At least one access level must be specified.', 'error');

            gForm.showFieldMsg('other_please_note', 'At least one access level must be specified.', 'error');

            return false; // Prevent submission

        }

    }

    return true;

}(current, gForm, gUser, gSNC);

Hi @vcaracci75 ,

 

I see exactly why your script is misbehaving. A few subtle issues are at play here:

 

Problems in your current script:

 

  1. Incorrect variable assignment

 

var requestType = gForm.getValue('read_only_groups_users');

 

  • You’re checking requestType against "Permissions Management", but you’re actually pulling the value of the read_only_groups_users field.
  • That means your condition will never be true unless that field literally contains "Permissions Management".
  • Most likely, you meant to check another field (maybe request_type or similar).

2.   Duplicate field usage

 

var readOnly = gForm.getValue('contribute_groups_users');     

var contribute = gForm.getValue('contribute_groups_users');   

 

  • Both readOnly and contribute are pulling from the same field (contribute_groups_users).
  • That disables your logic because you’re not actually checking the read_only_groups_users field.

3.  Function wrapping

 

}(current, gForm, gUser, gSNC);

 

  • That last line is an immediately invoked function expression (IIFE), which isn’t how ServiceNow client scripts work.
  • In a Client Script of type onSubmit, you should just define function onSubmit() { ... } and return true or false. No parameters, no invocation.

4.  UI Policies disabled

  • Returning false in an incorrectly structured onSubmit can interfere with UI Policies because the script is essentially short-circuiting the form submission lifecycle.

 

Corrected Script

 

function onSubmit() {

    // Get values from the three fields

    var requestType = g_form.getValue('request_type'); // <-- adjust to your actual field name

    var readOnly = g_form.getValue('read_only_groups_users');

    var contribute = g_form.getValue('contribute_groups_users');

    var other = g_form.getValue('other_please_note');

 

    // Only validate when request type is "Permissions Management"

    if (requestType === 'Permissions Management') {

        if (!readOnly && !contribute && !other) {

            g_form.showFieldMsg('read_only_groups_users', 'At least one access level must be specified.', 'error');

            g_form.showFieldMsg('contribute_groups_users', 'At least one access level must be specified.', 'error');

            g_form.showFieldMsg('other_please_note', 'At least one access level must be specified.', 'error');

            return false; // Prevent submission

        }

    }

 

    return true; // Allow submission

}

 

Key Fixes

  • Use g_form (not gForm) — ServiceNow client-side API is g_form.
  • Ensure you’re checking the correct field for "Permissions Management". Replace 'request_type' with the actual field name.
  • Assign each variable to the correct field (read_only_groups_users, contribute_groups_users, other_please_note).
  • Remove the (current, gForm, gUser, gSNC); invocation at the end. That’s not needed in client scripts.

Test this by:

  1. Setting request_type = "Permissions Management".
  2. Leaving all three fields blank → you should see error messages and submission blocked.
  3. Filling at least one field → submission should proceed normally, and UI Policies remain active.

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Best,

Anupam.

 

vcaracci75
Tera Expert

Hi Anupam,

This script works! One small change though. How do I get the error to only show up once at the top of the screen and not under each variable? (see screenshot)