eric.dizon
12th February 2013, 18:47
I have the code below for a custom session that I have created. The issue I am having when do I know that the session is doing an "Insert" or an "Update" reason being is I want to update my audit fields accordingly based on the state of the session before saving them. I only want to increment the first freee number I am using when it is "Insert" or new record being saved.

Create Date - tdsmi490.crdt, tdsmi490.crby
Last Modified - tdsmi490.lmdt, tdsmi490.lmby



choice.update.db:
before.choice:

if trim$(tdsmi490.suno) <> "" then |Survey No. is not blank.
db.retry.point()
select
tcmcs050.*
from tcmcs050 for update
where
tcmcs050._index1 = {"SUR", :tdsmi490.suno}
as set with 1 rows
selectdo

|Update First Freenumber
tcmcs050.ffno = tcmcs050.ffno + 1
tdsmi490.lmdt = utc.num()
tdsmi490.lmby = logname$
tdsmi490.suno = trim$(tdsmi490.suno) & edit$(tcmcs050.ffno, "999999")
tdsmi490.scre = calculate_score()

db.update(ttcmcs050,db.retry)
commit.transaction()
endselect
endif



Thanks and Regards,

Eric

mark_h
12th February 2013, 23:47
Check to see if this works update.status works from the baan help.
long update.status 4R
0 no update
ADD.SET during add
MODIFY.SET during modify
MARK.DELETE during delete

shah_bs
13th February 2013, 17:27
Another option is to program the logic in the main.table.io section. This is my personal preference.




main.table.io:

before.write:
|* place 'insert' related actions here.
|* example, increase first free number count.



before.rewrite:
|* place 'update' related actions here.



Do not use commit.transaction because the save action will do that for you.

eric.dizon
14th February 2013, 14:54
Thanks guys for the help. This will surely make my code cleaner.

BaanInOhio
14th February 2013, 15:50
If programming in LN, put your auditing logic in a DAL script for the table if custom or a user exit (UE) script if a standard table. This ensures that the auditing is done for any session, except GTM.

Use the "ue.after.after.save.object(mode)" hook in a UE script, using "mode" to differentiate between an insert (mode = DAL_NEW) and change (mode = DAL_UPDATE) if you only want to log changes to a different table. You will have to apply the insert/update through 'db.' or DAL call since the main table is already done applying changes at this point. If changes are applied to the main table of the session, put the updates in the "ue.before.before.save.object(mode)" hook. In this case, you only have to change the main table fields since the DAL will take care of the insert or update.

Use the "after.save.object(mode)" or "before.save.object(mode)" hook in a DAL for a custom table to perform your auditing logic. Mode settings and rules for using one or the other are the same as the UE usage.