get creation date and creator username of a record by calculated field

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
fgazza
Veteran Member
Posts: 205
Joined: 2019-04-30 17:37

get creation date and creator username of a record by calculated field

Post by fgazza » 2020-06-20 09:00

Hi everyone.
I would like to set up two queries directly in appgini for two calculated field.
In the first field, which I called "date", I would like the RECORD CREATION date to appear.
In the second field I would like the username of the user who created the record to appear.
Since for particular reasons the new records are generated in "update" mode and not in "create new" mode (ie there is a code that as soon as a user has logged in creates a record for the user and displays it in "update" mode) I can't set date and text fields with default values "<%%creationDate%%>" and <%%creatorUsername%%> but I have to set a query in the fields in "calculated field" mode.

Thanks for your help!

Fabiano

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: get creation date and creator username of a record by calculated field

Post by pbottcher » 2020-06-20 09:54

Hi.

why do you need to do this via calculated fields? As this is a one-time action you could handle it in the update function
check if there is already a "creation user" set, if not set it to the user and set the creation time. Otherwise do nothing.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

fgazza
Veteran Member
Posts: 205
Joined: 2019-04-30 17:37

Re: get creation date and creator username of a record by calculated field

Post by fgazza » 2020-06-20 10:50

thank you so much!

Can you please help me with the code? I'm a learner so is a bit diffisult form me!

Thanks!

Fabiano

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: get creation date and creator username of a record by calculated field

Post by pbottcher » 2020-06-22 18:49

Hi,

this is not possible without knowing your app.

If you need code support, please write me a pm.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: get creation date and creator username of a record by calculated field

Post by onoehring » 2020-06-23 07:04

Hi fgazza,

I agree with pbötcher on the why are you doing it this way in the fist place, and that it's impossible to help without knowing more details.
I suggest this:
Create created at/by fields as readonly in AG.
Then use php code in the /hooks/tablename.php -> after_insert and after_update and simply add code to write the logged in username and current timestamp to the recored (selected_id).
Note: you need to use after_ as you can not change the data for readonly fields in the hooks.

Get the username:

Code: Select all

$mi = getMemberInfo();  
$details = makeSafe($mi['username']);
Get the timestamp:

Code: Select all

$ts = date('Y-m-d H:i:s', strtotime("now"));
Just update the record:

Code: Select all

UPDATE `tabelname` SET `createdname`,`createdtime` VALUES ('$details','$ts') WHERE `ID` = ' . makeSafe(SelectedID) .';'
(code examples are not tested)

Olaf

Post Reply