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.

Prevent UI action modal from closing

Mike114
Kilo Contributor

I have a UI action with "Workspace Form Button" checkbox checked. In Workspace, I am able to click that button and enter data in a field. There two buttons: OK and Cancel. When hitting OK, I would like to do a field validation entered by a user. If the field has a certain value, I would like to throw an alert and prevent the modal from closing. Is this possible? Here is the code:
================================

function onClick(g_form) {

var fields = [{

type: 'string',

name: 'new_date',

label: getMessage('Enter new date:')

}

g_modal.showFields({

title: "Enter new date",

fields: fields,

size: 'md'

}).then(function(fieldValues){

if(fieldValues.updatedFields[0].value == '2023-05-19'){

    alert("Today's date is not allowed. Please enter another date");

    I would like to add something here that would prevent the modal from closing and stay on the modal form

}

else{

alert('Valid date has been entered');

}

}

3 REPLIES 3

sourav1999
Mega Guru

• Prevent the UI action modal from closing by using the "onBeforeClose" client script.

• The client script can be used to check whether the form is valid or not and prevent the modal from closing.

• The script can also be used to set a flag to indicate whether the form is valid or not.

• The flag can then be used to determine whether the modal should be closed or not.

• The onBeforeClose client script can also be used to execute a function before the modal is closed.

 

For asking ServiceNow-related questions try this :

For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.

Link - https://nowgpt.ai/

 

For the ServiceNow Certified System Administrator exams try this :

https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER

Ankita Kolhe
Tera Contributor

Hi @Mike114 ,

 

I have the same requirement. Would you able to achieve this?

Please let me know if it's possible to do validation & prevent modal to close.

 

Thanks & Regards,

Ankita

nataliya_b
Tera Guru

Hello @Ankita Kolhe and @Mike114 

Did you find the solution for this?