function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
phyerlightphyerlight 

Creating test data for read-only fields

Hi All,

I was wondering if there's any way to set the value of a read only field for the purposes of unit testing? In my case, I need to use check the Task.LastModifiedDate in a query and in order to test the query properly, I need to be able to set the last modified date to different values.

The only thing I can think of at the moment is to add a custom field that is assigned the value of the lastmodifieddate any time it is updated in a trigger. This of course isn't ideal.

If it can't be set, does anyone have any ideas or development patterns that got themselves around needing to do this?

Thanks.
mikefmikef
Use a functional test instead of just Unit tests.

In your test method just create a new task and update any of the editable fields on the task.
Check after insert and after update with your business logic.

All DML operations in testMethods do not get committed to the database, but live in memory and you can test against them.
phyerlightphyerlight
Thanks for the reply.

The problem with a functional test, I think, is that then the test would be dependent on the system state as it would be using some of the data already in the system would it not?

I am trying to create a new task and setting the fields that I need to properly test, but when I try to set the LastModifiedDate (a read only field) it reports an error and will not allow me to upload the file.

So I guess there isn't a way to get around it then? No magical salesforce function call that allows me to modify all fields for the purpose of testing? No ingenious technique used to get around relying on the last time a task was modified (Really what I need is when the task was closed and figured the last modified date would be close enough)? That's a shame then because it's difficult to create adequate time dependent test data when all the times are the same (or within milliseconds of each other).
mikefmikef
you can use the data that is there or create your own.

Create all the records you need for your testing.