Page 1 of 1
Clear subform Lookup field when another subform lookup field is changed (on the same row)
Posted: Thu Jan 05, 2023 3:24 pm
by incoherence
I have a subform with two Lookup fields, let's call them LOOKUP_A and LOOKUP_B
When the user changes the (looked up) value in LOOKUP_A, I want to clear (i.e. set to blank) LOOKUP_B on the same row.
I would like to do this in JavaScript if possible.
I am struggling with this simple task - any help appreciated... thanks!
Re: Clear subform Lookup field when another subform lookup field is changed (on the same row)
Posted: Fri Jan 06, 2023 7:01 am
by kev1n
Hi,
Add this JS in the LOOKUP_A's
LUJS event:
Code: Select all
const sfId = 'sf_id'; // subform object id
const lookupB = 'id_lookup'; // LOOKUP_B id
objLookupB = $('#' + sfId + nuPad3(nuSubformRow) + lookupB);
if (!objLookupB.value) {
nuGetLookupId(null, objLookupB.attr('id'), false, false);
}
If LOOKUP_B also needs to be cleared when LOOKUP_A is cleared, you'll need to add an onchange event as well.
Re: Clear subform Lookup field when another subform lookup field is changed (on the same row)
Posted: Sat Jan 07, 2023 4:01 am
by incoherence
I am not sure how it works exactly, but it does! Thanks so much, Kevin!
> const sfId = 'sf_id'; // subform object id
> const lookupB = 'id_lookup'; // LOOKUP_B id
> objLookupB = $('#' + sfId + nuPad3(nuSubformRow) + lookupB);
Here, is "nuSubformRow" a kind of "built-in" global variable that contains the (zero-based) subform row number?
I couldn't find nuPad3() but looks like it pads an integer to make it three digits (with leading zeros if required).
> if (!objLookupB.value) {
What is the purpose of this conditional, please?
(I think I need to go and read up more about jQuery to understand how the rest of the code works)
Re: Clear subform Lookup field when another subform lookup field is changed (on the same row)
Posted: Mon Jan 09, 2023 6:16 am
by kev1n
incoherence wrote: ↑Sat Jan 07, 2023 4:01 am
I couldn't find nuPad3() but looks like it pads an integer to make it three digits (with leading zeros if required).
Correct.
incoherence wrote: ↑Sat Jan 07, 2023 4:01 am
> if (!objLookupB.value) {
What is the purpose of this conditional, please?
! Is the logical NOT operator:
https://developer.mozilla.org/en-US/doc ... ogical_NOT
E.g. this
is the same as
Code: Select all
objLookupB.value != '' || objLookupB.value !== undefined || objLookupB.value !== null
The if statement checks if objLookupB.value is falsy (i.e., if it is null, undefined, 0, NaN, or an empty string). If objLookupB.value is not falsy, the code block inside the if statement is executed.