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();
}
}