Message is Disappearing

purdue
Kilo Sage

I have the submit client script below but message is disappearing after loading.   Any assistance is appreciated.  Thanks, Chad

function onSubmit() {
var assetTag = g_form.getValue('asset_tag');
var serialNumber = g_form.getValue('serial_number');
var regex = /\s+|\u200b/g;

// Check if asset_tag is not empty and trim whitespace
if (assetTag.match(regex)) {
var trimmedAssetTag = assetTag.replace(regex,'');
g_form.setValue('asset_tag', trimmedAssetTag);
g_form.addInfoMessage("We have trimmed your white space");
}

// Check if serial_number is not empty and trim whitespace
if (serialNumber.match(regex)) {
var trimmedSerialNumber = serialNumber.replace(regex,'');
g_form.setValue('serial_number', trimmedSerialNumber);
g_form.addInfoMessage("We have trimmed your white space");
}

}
1 ACCEPTED SOLUTION

purdue
Kilo Sage

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.

assettag.pngBusiness Rule.pngserial number.png

View solution in original post

18 REPLIES 18

neetusingh
Giga Guru

@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');

  }

}

 

Unfortunately this did not work.  Thanks though

Ankur Bawiskar
Tera Patron
Tera Patron

@purdue 

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.

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

Unfortunately this did not work.  Thanks