3

I'm trying to make a test class for an Apex class that runs a report and add functionality for it (send some emails and stuff). The problem is that I need to add records (contacts) for that report to work with, but I cannot make them fit the conditions required in the report.

The report conditions are: checkbox = TRUE AND edit date = TODAY

In the testMethod I do:

Contact c = new Contact(checkbox__c = FALSE);
insert c;
c.checkbox__c = TRUE;
update c;
System.debug('>>'+c.checkbox__c+', '+c.LastModifiedDate)
// run the report...

And this prints in the log:

USER_DEBUG [52]|DEBUG|>>TRUE, null

The checkbox its set to TRUE correctly but he LastModifiedDate is set to null when it was updated just now. The report runs correctly but says that it have not records to show and end the execution.

Thanks for the help.

7

You have to query back for the record.

Contact record = new Contact(SomeField__c=someValue);
insert record;

record.SomeField__c = otherValue;
update record;

record = [SELECT SomeField__c, LastModifiedDate FROM Contact WHERE Id = :record.id];
system.assertNotEquals(null, record.LastModifiedDate, 'This record has been modified');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.