UI Action
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 hours ago
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
her is my script for the UI action:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 hours ago
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
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.
Kaushal Kumar Jha - ServiceNow Technical Consultant - Rising Star/Class of Legends 2025
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
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();
}
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader