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
MTBRiderMTBRider 

Modifying LastModifiedDate for test classes

I have seen a couple posting on this from sometime ago, but have not seen any solutions.    I have logic that sends emails to owners of Opportunties that have not updated them for 30 days based on the LastModifiedDate of the record.  The code in my test class cannot trigger this condition because I am unable to insert a test record with a LastModifiedDate from 30 days ago.  Anyone have a solution for this?  Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
ShamilShamil

 

How are you handling the time-dependent event: through a time-based workflow action or do you have a scheduled apex code that periodically checks opportunities?

 

You may have to add a check into your actual code using Test.isRunningTest() to see if the code is executing in test mode and if so, not rely on the lastModifiedDate field, rather on something else that you can control through in the test code.

All Answers

ShamilShamil

 

How are you handling the time-dependent event: through a time-based workflow action or do you have a scheduled apex code that periodically checks opportunities?

 

You may have to add a check into your actual code using Test.isRunningTest() to see if the code is executing in test mode and if so, not rely on the lastModifiedDate field, rather on something else that you can control through in the test code.

This was selected as the best answer
MTBRiderMTBRider

Thanks for your reply.  It is batch Apex via the scheduler.  Let me give your suggestion of using isRunningTest() and see if I get anywhere with that.  Thanks again.

ShamilShamil

Ok, so if you use batch apex, my guess is that most likely the start() method runs a query and in the 'where' clause you're checking for the lastModifiedDate.

You can pass the query itself from the test class to your batch apex. The query would be modified not to rely on the lastmodifiedDate.

 

global class yourClass implements Database.Batchabale ... {
public String query; //you'd change this query when calling from the test class code
...
}

You can create opportunities with names set to 'mytestopty', and then have where clause: " where name = 'mytestopty' "

You'd then execute your batch apex from the test class. Pseudo-code is:

 

Test.startTest();
//insert your opportunities
//call batch apex:
ID batchprocessid = Database.executeBatch(yourBatchApexClassInstance);
Test.stopTest();

 

Check out Batch apex docs - scroll down to the 'Testing Batch Apex' section.

The key thing is - you won't be able to test the scenario that involves the lastModifiedDate in your unit tests, but you can execute the same code using different criteria

MTBRiderMTBRider

I could not do it in the Where clause because my batch class actually has a pretty generic query that pulls back a lot of opportunities.  There are a few conditions that I look for in the batch and then do different things based on what conditions are met.  I ended up just using the isRunningTest() method to meet this condition if running the test class like so:

 

if(o.Probability >= 90 && (o.LastModifiedDate.date().daysBetween(currentDay) >= 30 || Test.isRunningTest())) {
    //Do some stuff and junk
}

 That worked.  Thanks again for suggestion.