How to update createdAt and updatedAt fields in your data type?

Hi, 

For best practice I have added a createdAt and updatedAt field in my CDT. This is generally good to have on several of my CDTs to trace activity in the future.

How do I save a date/time to these fields with system generated data at the time of form submission? What are the recommended solutions for something like this? 

Many thanks, 

Eric

  Discussion posts and replies are publicly visible

  • +1
    Certified Lead Developer
    One solution would be to have an "Update Audit Fields" node in your process model before your data write.

    Here, you could save "now()" into updatedAt. For createdAt, you could use the following logic to save to your createdAt field:

    if(
    isnull(CDT.createdAt),
    now(),
    CDT.createdAt
    )


    Otherwise, you could have the same logic built into the submission button on your form.

    Personally, I don't like having a lot of code on the submit button since it's not always an obvious place to check. Meanwhile, looking through process history makes it very clear what's happening in your process model. Thus, I would recommend the first solution.
  • Something else you could also do is, instead of having createAt / updatedAt in your CDT, you can have the underlying database table written to have a CREATED_DATETIME / UPDATED_DATETIME field and in your database table schema specify the CREATED_DATETIME column to have a DEFAULT value of CURRENT_TIMESTAMP and the UPDATED_DATETIME column to have an ON UPDATE value of CURRENT_TIMESTAMP.

    The nice thing about this is you no longer have to enforce providing values for these fields at the application level, so if a new developer wants to write to these tables, they don't have to remember to fill in these values before writing to the table.