How to set Date/Time Opened and Closed in Salesforce Case record?

How to set Date/Time Opened and Closed in Salesforce Case record?

1. Enable “Enable “Set Audit Fields upon Record Creation” and “Update Records with Inactive Owners” User Permissions” in Salesforce User Interface in Setup.

2. Create a Permission Set. Enable “Set Audit Fields upon Record Creation”. Assign the Permission to the running user(User who will be setting Date/Time Opened and Closed).

Sample Code to Create a Case with Date/Time Opened and Closed:

insert new Case(
    Subject = 'Testing',
    Description = String.valueOf( System.now() ),
    CreatedDate = System.now().addYears( -1 ),
    Status = 'Closed',
    ClosedDate = System.now().addMinutes( -15 )
);

In the above example, the code was executed on 16th August 2022 at 08:00 AM. Date/Time Opened is set as 16th August 2021 08:00 AM and Date/Time Closed is set as 16th August 2022, 7:45 AM.

Leave a Reply