Page 1 of 1

Populate new record field from query result

Posted: Sat Mar 02, 2024 6:08 am
by woodhome
I know this must be easy to do, but I've gotten on the wrong track.
I have a form that has a field which, for new records, should default to the result of a query. I created a simple procedure:
with the code:
code:NextXYZId
PHP:

Code: Select all

$s  = "SELECT CONCAT('XYZ',LPAD(max(RIGHT(xyz_id,6)+1),6,'0')) as newXYZid FROM all_contacts;";
$t  = nuRunQuery($s);

if (db_num_rows($t) == 1) {
    $r  = db_fetch_object($t);
    nuSetFormValue('xyz_id', $r->newXYZid);
}

Then in the custom code of the xyz_id input object of the edit form I have:
onnuload :
if (nuIsNewRecord()){
console.log("It is a new record");
nuRunPHPHidden("NextXYZId");
}

Obviously this isn't right but how is it supposed to work?

Re: Populate new record field from query result

Posted: Sat Mar 02, 2024 6:54 am
by kev1n
Hi,

nuSetFormValue() can only be used in the "After Browse" event of a Lookup object.
I'd use the form's 'Before Edit' event and nuAddJavaScript() as shown in this Code Library article.

Re: Populate new record field from query result

Posted: Sun Mar 03, 2024 6:00 am
by woodhome
Perfect! Just the guidance I needed.