Check if oldValue was empty/null in onChange client script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2021 06:25 AM
How can I check if the oldValue is empty or null? I have a client script that clears values of multiple fields, but I only want it to clear those fields IF the oldValue of the field wasn't empty and actually held a value.
So for instance, I have three fields:
Field A | Reference |
Field B | Reference |
Field C | Reference |
When 'Field A' changes value, it will clear 'Field B' and 'Field C'. I only want this to clear values though IF Field A wasn't empty previously. So if I Populate Field C and Field B before populating Field A, I don't want it to clear those fields because Field A didn't have a value previously.
I've tried the following with no luck:
if (!oldValue) {
g_form.clearValue('Field_B');
g_form.clearValue('Field_C');
}
if (oldValue != null) {
g_form.clearValue('Field_B');
g_form.clearValue('Field_C');
}
if (oldValue != '') {
g_form.clearValue('Field_B');
g_form.clearValue('Field_C');
}
Nothing seems to work. I only want it to clear those values if there was a value in 'Field A' previously.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2021 10:13 AM
This script works onChange of Field_A, provided the field names (not labels) of Field_B and Field_C are correct (case-sensitive). Keep in mind that oldValue will always be the value of Field_A when the form loads. So if Field_A is populated when the record loads, then you blank it, then populate B and C, then populate A, B & C will still get cleared. If you are populating Field_A with a default value or onLoad script, you'll have to test with an alert to see what oldValue is.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(oldValue != ''){
g_form.clearValue('Field_B');
g_form.clearValue('Field_C');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2021 07:01 AM
Did this answer your question?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2021 08:56 AM
Hey Brad, no sir, if you noticed I tried that as a solution already. All three of the solutions I showed in my original post failed. It seems nothing identifies the oldValue as being empty, including the one you added(which is my third tried example).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-06-2021 03:41 AM
My solution is tested and working given the understanding and conditions explained in my response. If it is not working for you then you either have incorrect field name(s) or are doing something different than the test case that I outlined.