Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

nuHasNotBeenEdited() has no effect on nuGetLookupId()

Questions related to using nuBuilder Forte.
Post Reply
ajackson
Posts: 19
Joined: Fri Mar 22, 2024 4:59 pm

nuHasNotBeenEdited() has no effect on nuGetLookupId()

Unread post 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
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

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

Unread post 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();
    }
}
ajackson
Posts: 19
Joined: Fri Mar 22, 2024 4:59 pm

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

Unread post 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
kev1n
nuBuilder Team
Posts: 4292
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 444 times
Contact:

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

Unread post by kev1n »

Call nuGetLookupId() with these parameters (3rd param: set focus, 4th param: set focus)

Code: Select all

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