Procedure won't fill field value from variable

Why won’t this NewRecord procedure auto-fill (NSN and Date fields) data cells? See zlog below…
PROCEDURE:
showallfields
selectall
field NSN
sortup
lastrecord
zlog "NSN is "+NSN
nsnotify NSN
local lNSN
lNSN = NSN+1
showvariables lNSN
zlog "lNSN is "+lNSN

Addrecord
field NSN
cell lNSN
zlog "Added record; field NSN is: "+ NSN
field Date
cell superdatepattern(supernow(),"MM/DD/YY @ ",“hh:mm:ss am/pm”)
zlog "Date recorded as "+Date
field «Name of recipe»
zlog “End of .NewRecord procedure”
call .ModifyRecord
••••• zlog record for this procedure:
[TIME: 2:42 PM]
[CookBookPanX210107/.NewRecord] NSN is 1412
[CookBookPanX210107/.NewRecord] lNSN is 1413
[CookBookPanX210107/.NewRecord] Added record; field NSN is: 0
[CookBookPanX210107/.NewRecord] Date recorded as 0
[CookBookPanX210107/.NewRecord] End of .NewRecord procedure

I can’t really tell from the limited information provided. Probably something to do with the data types of the fields.

I do have some thoughts. I think you are wanting to automatically generate a new highest number for the NSN field. Instead of sorting, I would suggest this one line of code.

let INSN = aggregate("NSN","max")+1

By the way, this line in your code is definitely unecessary

showvariables INSN

You’ve defined INSN as a local variable, and local variables can never be displayed. So there is no point in using the showvariables statement with a local variable (only a fileglobal variable).

What type of field is Date? Is it a date field, or a text field? Because your code is attempting to put text into it. If it’s a date field, then zero is exactly what would be expected.


Assuming NSN is an integer field and Date is a date field, here is my suggestion for a rewritten version of this code.

showallfields
selectall
let INSN = aggregate("NSN","max")+1
addrecord
NSN = INSN
Date = today()
call .ModifyRecord

Actually, the local variable really isn’t needed, so it could be even simpler:

showallfields
selectall
addrecord
NSN = aggregate("NSN","max")+1
Date = today()
call .ModifyRecord

Hmm, I just realized this is a .NewRecord procedure. So I’d definitely recommend this simpler version that doesn’t re-sort the entire database every time a new record is added. As a user, that re-sort would be super annoying.

Wow! Thanks, Jim.