UI Action

sparkles
Tera Contributor

Hi,

 

I created UI action to set the ticket to closed skipped. It works fine, but it leaves the comments field empty. I want to have a pop-up message or alert "Please fill in the Comments field before closing"

See below how the ticket is closed but the comments field still empty

 

 

sparkles_0-1765929243675.png

her is my script for the UI action: 

current.state = 7
current.update();
 
Its on a custom table
 
Thanks!
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@sparkles 

you can't throw alert in your client side script

Make your UI action as client side instead of server side and then use this logic

something like this

// Client side onclick function
function closeTicket() {
    if (g_form.getValue('comments') == '') {
        g_form.setMandatory('comments', true);
        alert('Please fill in the Comments field before closing');
    }
    g_form.setValue('state', '7'); // Closed Skipped
    g_form.save();
}

AnkurBawiskar_0-1765939829375.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

You could do something similar to the Reject UI Action which requires a comment when rejecting an approval:

var comments = false;
if(current.comments.getJournalEntry( 1) != undefined) {
	var lastComment = current.comments.getJournalEntry( 1);
	var index = (current.comments.getJournalEntry( 1)+'').indexOf(gs.getUserDisplayName());
	if (index > -1 && index <= 22)
		comments = true;
}
if(!JSUtil.nil(current.comments) || comments){
    current.state = 7;
    current.update();
}else{
	gs.addErrorMessage(gs.getMessage("Please fill in the Comments field before closing"));
	current.setAbortAction(true);
}

 

Abinaya Sekar
Tera Expert

To fix this, the UI Action is converted to a client-side UI Action, which can read what the user typed on the form.

 

UI Action Script (Client-side)
function onClick() {
var comments = g_form.getValue('comments');

if (!comments || comments.trim() === '') {
alert('Please add a comment before closing this ticket.');
return false;
}

g_form.setValue('state', '7'); // Closed Skipped
g_form.save();
}

 

 

To ensure comments are always required, even from other update paths:( Business Rule ) \

 

if (current.state == 7 && current.comments.nil()) {
gs.addErrorMessage('Please add a comment before closing this ticket.');
current.setAbortAction(true);
}

kaushal_snow
Giga Sage

@sparkles ,

 

It is because your current ui action code runs server side and updates the record without ever checking the comments field first, so even if it’s empty it sets state to closed skipped and saves.........to prevent that you need to add a client side check in the ui Action (check the Client box and use an onclick function)........that validates g_form.getValue('comments') before allowing the submit, and if it’s empty do an alert(please fill in the comments field before closing) or show a field message and return false so the server script never runs.........

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Technical Consultant - Rising Star/Class of Legends 2025

Ankur Bawiskar
Tera Patron
Tera Patron

@sparkles 

you can't throw alert in your client side script

Make your UI action as client side instead of server side and then use this logic

something like this

// Client side onclick function
function closeTicket() {
    if (g_form.getValue('comments') == '') {
        g_form.setMandatory('comments', true);
        alert('Please fill in the Comments field before closing');
    }
    g_form.setValue('state', '7'); // Closed Skipped
    g_form.save();
}

AnkurBawiskar_0-1765939829375.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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