Is there a way to set SystemModstamp on a newly created SObject in a unit test?

I need to create a SObject in a unit test with the SystemModstamp set. I’ve been using Opportunities in my unit test, but I am not tied to that specific SObject. I also am not tied down to SystemModstamp (though that is what my app used in production). I really just need to be able to modify a DateTime field on a SObject. Ideally though, I would be able to set the SystemModstamp or cause it to be set. I tried inserting the newly created Opportunity, but it looks like that insert doesn’t populate the SystemModstamp.

Answer

The SystemModStamp will always be set. If you requery the object in your test class it should get you the SystemModStamp:

Account a = new Account();
a.Name = 'name';
insert a;

system.debug(a.systemModStamp);
system.debug([select Id, SystemModStamp From Account Where Id = :a.Id]);

returned:

|DEBUG|null
|DEBUG|(Account:{SystemModstamp=2014-04-03 19:08:12, Id=001c000000Y36ucAAB})

Attribution
Source : Link , Question Author : edgartheunready , Answer Author : Guy Clairbois

Leave a Comment