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

@purdue 

why are you not using onChange client script and trimming and then setting the value?

OR

You can write the logic to trim after form submission

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

I tried the OnChange but issue still persists.  Just to be clear the message is displaying but disappears once the form loads. Please provide example to display message then trim after submission.  Thanks, Chad

Hello @purdue 

function onSubmit() {
 var assetTag = g_form.getValue('asset_tag');
 var serialNumber = g_form.getValue('serial_number');
 var regex = /\s+|\u200b/g;
 var trimmed = false;
 // Trim asset tag
 if (assetTag.match(regex)) {
   g_form.setValue('asset_tag', assetTag.replace(regex, ''));
   trimmed = true;
 }
 // Trim serial number
 if (serialNumber.match(regex)) {
   g_form.setValue('serial_number', serialNumber.replace(regex, ''));
   trimmed = true;
 }
 if (trimmed) {
   // Show confirmation popup and prevent immediate submit
   var message = "We have trimmed your white space. Click OK to continue submitting.";
   g_form.addInfoMessage(message);
   // Show popup, wait for OK
   setTimeout(function() {
     var confirmed = confirm(message); // referred: https://www.w3schools.com/jsref/met_win_confirm.asp
     if (confirmed) {
       // Submit the form manually
       g_form.submit();
     }
   }, 0);
   return false; // Stop submission until user confirms
 }
 return true; // Proceed with submission if no trimming needed
}



VishalJaswal_2-1747153737916.png

 

VishalJaswal_3-1747153742520.png

 


Hope that helps!

Hello,

Thank for this.   Is there anyway we can pop the message without the confirmation?   They just want the user to see the message.

Thanks,

Chad

Juhi Poddar
Kilo Patron

Hello @purdue 

Try this onSubmit client script:

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_modal.alert("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_modal.alert("We have trimmed your white space");
    }
}

Note: Replacing g_form.addInfoMessage() with g_modal.alert()

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar