Why i am getting same alert and error message twice in workspace?

KM SN
Tera Expert

I have configured on change client script to clear the dob value and to show alert or error message if the selecting date is in future. I tried with alert and error message both are reflecting twice.

 

when i tried with field error box its clearing the value first time and showing error box but on same page second time also if i have choose future value its not clearing the value why?

 

Can i have any inputs on it?

 

Client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
 
    var today = new Date();
//var currentDate = today.format('MM/DD/YYYY');
    var dob = g_form.getValue('date_of_birth');
 
alert(today);
alert(dob);
 
    if (dob < today ) {
 
        g_form.addInfoMessage('dob is less than today');
        return;
 
 
    } else {
 
 
g_form.addErrorMessage('DOB can not be future date!!');
g_form.clearValue('date_of_birth');
        //g_form.showErrorBox('date_of_birth', 'DOB can not be future date!!');
    }

reporter.PNG

9 REPLIES 9

KM SN
Tera Expert

Hi @Hemanth M1....  gone through same feeling!!

 

Just check below on change modified client script which is working fine in native UI but not working as expected  in workspace.

 

if dob is greater than today date its clearing the value with out error msg.

second time on same page again when i selected future date the value was there at field but not cleared.

 

The above and this both are very strange to me why this is happening only in workspace.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '' || newValue == null) {
return;
}

// Type appropriate comment here, and begin script below

var today = new Date();
var dob = new Date(g_form.getValue('date_of_birth'));

var view = g_form.getViewName();


if (today < dob) {

g_form.clearMessages();
g_form.clearValue('date_of_birth');
g_form.showErrorBox('date_of_birth', 'DOB can not be future date!!');

}

else {

return true;

//console.log(newValue);
//alert('Date of birth allows only past date');
//g_form.setValue('date_of_birth','');
//alert('DOB can not be future date!!');


}

}

Hi @KM SN ,

 

It a issue with g_form.clearValue('date_of_birth'); statement, workspace considering this is also a change to the field and executing script multiple times,

 

if you comment this line it works fine but it will not clear the field.

 

write g_form.clearMessages(); before the if else block (so that it would clear the messages when u change the date form past to future /From future to past

 

//replace below script in the if block.

g_form.addErrorMessage("DOB can not be future date!!")
g_form.setValue("date_of_birth"" ");
 
This works when u change the DOB to future date (it will clear the field vale aswell), if u change the field value again on the same page it shows the message but you could see the value (no worries its not actual value , even if u save the form it will go back to the previous value)
 
Thank you,
Hemanth
Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

KM SN
Tera Expert

Hi @Hemanth M1 

 

Thanks for inputs but its not a expected behavior. 

 

when i did as you said  first time i am getting message and value is clearing  whereas second time i am just seeing message but value is not clearing  and when i save the record it taking the value which is not a expected thing.

 

I will be happy if you come up with any more inputs which solve this strange behavior.

 

Thank You.

Mani

Hi @KM SN ,

 

Would you mind try this solution (i haven't verified ) 

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-... 

 

I still see clearValue, let see how it behaves in UI policy script.

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

lcc
Tera Expert

Hi, I have the same issue as well where the onChange script runs twice in configurable workspace but not in backend UI. The issue appears when I clear the same onchange field after some actions being performed. However, when I delay the clearField (for 2 seconds), the onChange does not run twice in workspace.  This is the source codes I tried: 

if (isLoading || newValue === '') {
return;
}

var setTimer = '';
setTimer = setTimeout(setTimerDelay, 2000); 
function setTimerDelay() {
alert('timer waits for 2 sec');
g_form.clearValue(<field_name>);
}

I was literally deleting all codes while trying the clearValue function on the form. 😞