Page 1 of 1

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

Posted: 2020-06-20 09:00
by fgazza
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

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

Posted: 2020-06-20 09:54
by pbottcher
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.

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

Posted: 2020-06-20 10:50
by fgazza
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

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

Posted: 2020-06-22 18:49
by pbottcher
Hi,

this is not possible without knowing your app.

If you need code support, please write me a pm.

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

Posted: 2020-06-23 07:04
by onoehring
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