How to display an error message when condition(s) are no me

ID NOBLE
Tera Expert

Please I have a requirement to display an error message "Must provide a reason(s) for your selection".

 

We have a variable of type "SELECT BOX" with options: True, False, Unknown. So, when a user select "False" from these options, the above error message (in red color) must automatically display to users. Please how do I do this?

 

Thanks.

1 ACCEPTED SOLUTION

@ID NOBLE 

 

You can make use of an On-Submit Catalog Client Script. Please refer below screenshots :

AmitVerma_0-1733893530043.png

 

 

function onSubmit() {
    var userChoice = g_form.getValue('user_choice'); // Please replace with your catalog variable
    if (userChoice == 'false') {
        g_form.addErrorMessage('Please provide a reason for your selection before submitting');
        return false;
    }
}

 

 

AmitVerma_1-1733893587402.png

 


Please mark this response as correct and helpful if it assisted you with your question.

View solution in original post

12 REPLIES 12

AshishKM
Kilo Patron
Kilo Patron

Hi @ID NOBLE ,

If this is for any catalog item, then write catalog client script [onChange] on select box varilables.

In the script code check the select box variable value and display the message ( Info or Error ). Error message shows in red.

 

To create an onChange client script for a ServiceNow catalog item with a select box that displays an error message based on the selected value, follow these steps:

  1. Navigate to your catalog item in ServiceNow.
  2. Scroll down to the "Catalog Client Scripts" related list and click "New".
  3. Set the "Type" to "onChange".
  4. In the "Field name" field, enter the name of your select box variable.
  5. In the "Script" field, enter the following code:

  

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    if (newValue == 'False') {
        g_form.addErrorMessage('Must provide a reason(s) for your selection.');
    } else {
        g_form.clearMessages();
    }
}

 

 

-Thanks,

AshishKM

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

@AshishKM,

 

Thanks for your response. I have just followed the above steps, but it does not work. Can you please help check again?

Once again, thanks.

Please share more detail like, where you are using this script.

What's the select box variable name and its choice value.

Add some screen shorts.


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

@AshishKM

 

Thank you. It's for a variable set and the variable name is "Has the form been filled and signed? (True/False).

 

Expected error message if FALSE is selected: Must provide a reason(s) for your selection