onChange client script IF statement help

Steve42
Tera Expert

I have a pretty simple onChange Script that is working, however I only need it to update the field if there is not a previous value in there. sort of like if (field==''){ do something} but that's not working and giving me an error.

Here is the working script:  Could someone help with how to set it to run only if the field that its trying to update is null

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

   //Type appropriate comment here, and begin script below
	var today_dateStr = formatDate(new Date(),g_user_date_time_format);
	//alert(today_dateStr);
	g_form.setDisplay('u_tirage_sla_time',false);
		g_form.setReadOnly('u_triage_sla_time', true);
        g_form.setValue('u_triage_sla_time', today_dateStr);
}
1 ACCEPTED SOLUTION

The SN Nerd
Giga Sage
Giga Sage

Add an if statement checking the value of the field

if (g_form.getValue('u_triage_sla_time') == ’’) {

}

 

Also, there is a typo in your setDisplay function


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

4 REPLIES 4

The SN Nerd
Giga Sage
Giga Sage

Add an if statement checking the value of the field

if (g_form.getValue('u_triage_sla_time') == ’’) {

}

 

Also, there is a typo in your setDisplay function


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Perfect, also thank you for the spelling check, I would have missed that.

Sajilal
Mega Sage

Try This!


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

//Type appropriate comment here, and begin script below
var today_dateStr = formatDate(new Date(),g_user_date_time_format);
//alert(today_dateStr);

if(today_dateStr=="")

{
g_form.setDisplay('u_tirage_sla_time',false);
g_form.setReadOnly('u_triage_sla_time', true);
g_form.setValue('u_triage_sla_time', today_dateStr);
}

}

asifnoor
Kilo Patron

Hi

In your OnChange client script, remove this condition from code block.

|| newValue === ''

Then when the value is empty also, it will work.