The CreatorCon Call for Content is officially open! Get started here.

How to set a Incident field mandatory using ui action

Bruce lee
Tera Contributor

How to set a Incident field mandatory using ui action What's the script for this

4 REPLIES 4

Community Alums
Not applicable

Hi @Bruce lee ,

 Why do you want to use a UI action to make any field manadatory?

As per the best practice we should be using UI Policy whihc doesn't involve any scripting.

learn more from the link : UI policies 

Ankur Bawiskar
Tera Patron
Tera Patron

@Bruce lee 

your UI action should be client side to make field mandatory

sample script

if(g_form.getValue('fieldName') == ''){
	g_form.setMandatory('fieldName', true);
}

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Bruce lee 

Hope you are doing good.

Did my reply answer your question?

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Debasis Pati
Tera Guru

Hello @Bruce lee ,

You can try the below type of script script

// Get the value of the field
var fieldValue = current.field_name;

// Check if the field value is blank
if (!fieldValue) {
// Add an error message to the form
gs.addErrorMessage('Please fill in the field name before submitting the form.');

// Make the field mandatory
g_form.setMandatory('field_name', true);

// Stop the form from being submitted
action.setRedirectURL(current);
}

Just amend the conditions depending upon your requirement and field names.
Please mark it as helpful/correct if this helps you.

Regards,
Debasis