Page 1 of 1

nuHasNotBeenEdited() has no effect on nuGetLookupId()

Posted: Wed Jun 19, 2024 12:35 pm
by ajackson
Hi,
I have the following code in the "Edit" section of a form's custom code:

Code: Select all

if (nuIsNewRecord()) {
    nuSetValue('PacketNr','0');
    nuGetLookupId('0', 'PatientID');
    nuHasNotBeenEdited();
}
When I attempt to leave or refresh a new record on this form without changes the message "Leave this form without saving?" appears.
This is unexpected since "nuHasNotBeenEdited" has been called at the end of the code.
However if I comment out the call to "nuGetLookupID" no message is produced.

Cheers,
Alan

Re: nuHasNotBeenEdited() has no effect on nuGetLookupId()

Posted: Wed Jun 19, 2024 2:26 pm
by kev1n
Hi Alan,

The issue arises because nuGetLookupId() is an asynchronous function, meaning it doesn't wait for the lookup process to complete before moving on to the next line of code. This can result in nuHasNotBeenEdited() being called before nuGetLookupId() has finished, which may cause the form to be marked as edited and trigger the "Leave this form without saving?" message.

To address this, you need to ensure that nuHasNotBeenEdited() is called only after nuGetLookupId() has completed its operation. This can be achieved by using a callback function, nuOnLookupPopulated(), which is called when nuGetLookupId() finishes.

Here is how you can modify your code to include the callback (untested):

Code: Select all

// Initialize a flag to track if default values need to be set
var setDefault = false;

// Check if the current record is a new one
if (nuIsNewRecord()) {
    // Set the default value for 'PacketNr' to '0'
    nuSetValue('PacketNr', '0');

    // Set the flag to indicate default values are being set
    setDefault = true;

    // Initiate the lookup process for 'PatientID'
    nuGetLookupId('0', 'PatientID');
}

// Callback function that is called when the lookup process is completed
function nuOnLookupPopulated(id, p) {
    // Check if the default values are being set and if the lookup ID is 'PatientID'
    if (setDefault && id === 'PatientID') {
        // Reset the flag as default values have been set
        setDefault = false;

        // Call nuHasNotBeenEdited() to correctly mark the form as unedited after the lookup is populated
        nuHasNotBeenEdited();
    }
}

Re: nuHasNotBeenEdited() has no effect on nuGetLookupId()

Posted: Thu Jun 20, 2024 10:46 am
by ajackson
Hi Kevin,

I suspected this might be the case. I've tried your suggestion, though I couldn't get it to work.
However if I introduce a one second delay before executing nuHasNotBeenEdited() then it works most of the time.

Code: Select all

        setTimeout(function() {
            nuHasNotBeenEdited();
        }, 1000);
nuOnLookupPopulated() also seems to catch nuHasNotBeenEdited() before it finishes.

Cheers,
Alan

Re: nuHasNotBeenEdited() has no effect on nuGetLookupId()

Posted: Thu Jun 20, 2024 3:17 pm
by kev1n
Call nuGetLookupId() with these parameters (3rd param: set focus, 4th param: set focus)

Code: Select all

nuGetLookupId('0', 'PatientID', false, false);