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.

2 REPLIES 2

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);