- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2020 06:45 AM
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);
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2020 06:54 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2020 06:54 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2020 07:19 AM
Perfect, also thank you for the spelling check, I would have missed that.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2020 06:55 AM
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);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2020 06:58 AM
Hi
In your OnChange client script, remove this condition from code block.
|| newValue === ''
Then when the value is empty also, it will work.