- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2025 08:53 PM
I have the submit client script below but message is disappearing after loading. Any assistance is appreciated. Thanks, Chad
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2025 11:43 AM
Hello All,
I want to thank everyone for their responses. We were able to resolve this. Below is the solution.
Ask
Filter Whitespace from 2 fields(Asset Tag and Serial Number) on Alm Asset.
Solution
Create 2 OnChange Client Scripts AND Before BR on Insert and Update with condition gs.Interactive for editing from list view.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2025 09:35 PM
@purdue -
The issue occurs because info messages added with g_form.addInfoMessage() in a submit client script disappear immediately after the form reloads or submits. This happens because the page refresh clears those messages.
Why?
The onSubmit script runs just before submission, and when the form submits successfully, the page reloads or navigates away, removing any displayed messages. Consequently, the messages appear only briefly or not at all.
How you can fix this -
Option 1: Use g_form.showFieldMsg() instead of addInfoMessage()
showFieldMsg() attaches a message directly to a specific field and stays visible until the form reloads or the field is changed.
function onSubmit() {
var assetTag = g_form.getValue('asset_tag');
var serialNumber = g_form.getValue('serial_number');
var regex = /\s+|\u200b/g;
// Check and trim asset_tag
if (assetTag.match(regex)) {
var trimmedAssetTag = assetTag.replace(regex, '');
g_form.setValue('asset_tag', trimmedAssetTag);
g_form.showFieldMsg('asset_tag', 'We have trimmed your white space', 'info');
}
// Check and trim serial_number
if (serialNumber.match(regex)) {
var trimmedSerialNumber = serialNumber.replace(regex, '');
g_form.setValue('serial_number', trimmedSerialNumber);
g_form.showFieldMsg('serial_number', 'We have trimmed your white space', 'info');
}
}
Option 2: Use onChange client scripts instead of onSubmit
If you want to notify users immediately when they enter whitespace, consider trimming and showing messages on the field's onChange client script instead of on submit.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') return;
var regex = /\s+|\u200b/g;
if (newValue.match(regex)) {
var trimmedValue = newValue.replace(regex, '');
g_form.setValue(control.name, trimmedValue);
g_form.showFieldMsg(control.name, 'We have trimmed your white space', 'info');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2025 08:25 AM
Unfortunately this did not work. Thanks though
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2025 09:41 PM
that's platform behavior, info messages get disappeared after some time
You can write onChange to trim the whitespace on respective fields and show the field message on that field
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2025 08:25 AM
Unfortunately this did not work. Thanks