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

6 REPLIES 6

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.

 

Me Being Mustaq
Tera Guru
Hi @vcaracci75 ,
 
Use g_form.addErrorMessage() in a Catalog Client Script to display validation errors that prevent form submission in Requested Items
 
onSubmit Client Script Setup
Catalog Client Script on your Requested Item:
  • Type: onSubmit
  • Applies to: Your SharePoint site item
function onSubmit() {
    // Example: Check if request_type is Permissions Management
    if (g_form.getValue('request_type') == 'permissions_management') {
        
        // Check if at least one Level of Access is selected
        var readAccess = g_form.getValue('read_access');
        var contributeAccess = g_form.getValue('contribute_access');
        var fullControl = g_form.getValue('full_control_access');
        
        var hasAccess = (readAccess == 'true') || 
                       (contributeAccess == 'true') || 
                       (fullControl == 'true');
        
        if (!hasAccess) {
            g_form.addErrorMessage('At least one Level of Access must be selected for Permissions Management requests.');
            return false; // Blocks submission
        }
    }
    
    return true; // Allows submission
}
Error Message Features
  • Red banner appears at top of form
  • Prevents submit when return false
  • Field highlighting (optional with g_form.setMandatory() or g_form.showFieldMsg())
  • Clears automatically on field change or page refresh
Field-Level Error
For specific field focus/highlighting:
g_form.showFieldMsg('read_access', 'Select at least one access level', 'error');
g_form.showFieldMsg('contribute_access', 'Select at least one access level', 'error');
 
This creates inline red error messages under specific variables
 
Please refer to the below link:-
 
 
If it is helpful, please hit the thumbs button and accept the correct solution by referring to this solution in the future it will be helpful to them.
 
Thanks & Regards,
Shaik Mustaq.