+ Start a Discussion

How to set creation date

It seems like something obvious but it took me hours to find a way to set a DateTime field to the time when a record is created.   I am building a simple Change Managment system as my first real project in Salesforce. As the administrator I want a method for my users to request changes and log defects and I need to let them know when changes are deployed.

So I created a single custom object with a "Date Opened" field.


When the record is created I want this field to get the current time and then never to change again.


Solution (partial) create a trigger and use this code. Replace Change_Request__c with the name of your object.  Replace the Date_Opened__c field name too.


This trigger only fires when the record is inserted, or created.


Triggers operate in batch so it may process a set of records. That is why the assignment is in a for loop.


System.now() returns the local date time.


This is only a partial solution because the page layout of my custom object is not read only. Users can edit the date!.  I have set the field level permission to read-only but that didn't help. When, and if, I figure this out then I'll post back.



trigger SetOpenDate on Change_Request__c (before insert) {
    for (Change_Request__c chg : Trigger.new) {
        // Iterate over each sObject  
        chg.Date_Opened__c = System.now();
Best Answer chosen by Admin (Salesforce Developers) 

I was using a Sys Admin account so that is why it could modify a read only field.  Thanks for explaining this.

All Answers


More about making a Custom Field read-only.

Setup .. Create .. Object ... select custom object. Scroll down and select custom field. Title of section now says Custom Field Definition Detail. Press Set Field Level Security and set all Read-only check boxes.


Return to edit an instance of the custom object and the date field can be selected. A calendar pops up and the value can be changed.


So, edit page layout and select the date field. Click on the wrench to set properties and select read-only.

The field can still be modified by user.


Back to looking for a way to make the field unmodifiable.


Users with modify all data perm can write to fields marked as readonly, you're probably running into this.


Is there some reason the standard system supplied created date field doesn't work for you ?


Thanks for the reply.   I'll look into your suggestion about the users having modify all permissions.  Could this be something that happens in a sandbox only?


Why not use "created date"?  Good question.  I want my own "opened date" as a sort of a "hello world" trigger that will get me started.  I expect to write more involved triggers soon.  I also wanted to name the field with the word "open" so it will relate to other fields and descriptions.  Perhaps there is a way to rename standard fields, in the UI?


I considered using a formula field but I could not see how to keep the value from changing on each update.


Also,for this simple date value, we could use a default value expression "NOW()" or "TODAY()" for just date values.  Right?


I was using a Sys Admin account so that is why it could modify a read only field.  Thanks for explaining this.

This was selected as the best answer