I think i have run into a peculiar bug. There are two ways to run test code:

1) Setup -> Inbound change sets -> Select change set -> Validate button or deploy button.
2) Setup -> Develop -> Apex test execution -> Run test code

The first is useful when deploying new code, the second is useful to make sure changes haven't broken anything. Functionally these two should do exactly the same thing, and they don't.

These two methods behave differently for the last viewed date of a record. When i use the validate button or deploy button then i can't get information about the last viewed date. This is true even if i run the code as a user, and use test.startest() and test.stoptest().

Next, i deploy the code. To do this i have to write my test code in such a way that i can deploy the code even without having access to the last viewed date.

When the code is deployed, i can use the second method to run the same test class. Strangely enough i then get access to the last viewed date. This i believe is a very strange result?

In order to reproduce this, one can simply take the code i have provided and check the debug logs. When using the deploy button in a production org none of the last viewed dates will be visible. After the code is deployed, the second method of running test code can be used. Then the debug logs will show the last viewed date.

Code example:

public class temp_oddBehaviourLastViewedDate {

    private static testMethod void runTests(){

        // Create user
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 

        User u = new User(Alias = 'sysadmin', 
                          ProfileId = p.Id, 
        insert u;

        // Run as user

            //Create contact
            contact c = new contact(lastname='name contact');
            insert c;

            // Check if lastvieweddate is set (in my test org it is immediately after creation).
            system.debug('Contact last viewed date before for view ' + [select id, lastvieweddate from contact]);

            // I really want it to be viewed, so let's view it.
                contact[] cs = [select id, lastvieweddate from contact for view];
                system.debug('Contact last viewed date after for view during test.startest' + [select id, lastvieweddate from contact]);

            system.debug('Contact last viewed date after for view after test.startest' + [select id, lastvieweddate from contact]);



While validating i get this result in my debug log:
Contact last viewed date before for view (Contact:{Id=003b000001bdz9vAAA})
Contact last viewed date after for view during test.startest(Contact:{Id=003b000001bdz9vAAA})
Contact last viewed date after for view after test.startest(Contact:{Id=003b000001bdz9vAAA})

After deploying i get this:
Contact last viewed date before for view (Contact:{Id=003b000001bdzFEAAY, LastViewedDate=2017-02-16 19:29:06})
Contact last viewed date after for view during test.startest(Contact:{Id=003b000001bdzFEAAY, LastViewedDate=2017-02-16 19:29:06})
Contact last viewed date after for view after test.startest(Contact:{Id=003b000001bdzFEAAY, LastViewedDate=2017-02-16 19:29:06})

Please tell me i'm not going insane ;)? If i'm missing something than i'd love to know! Thank you and sorry for the long post!


You need to include a FOR VIEW clause in your query (emphasis mine):


Salesforce stores information about record views in the interface and uses the information to generate a list of recently viewed and referenced records, such as a list of records in a sidebar and for a list of records as auto-complete options in search. You can update objects with information about when they were last viewed by using the FOR VIEW clause in a SOQL query.

Consider using the FOR VIEW clause in conjunction with the FOR REFERENCE clause to update recent usage data for retrieved objects.

When this clause is used with a query, two things happen:

  • The LastViewedDate field for the retrieved record is updated.
  • A record is added to the RecentlyViewed object to reflect the recently viewed data for the retrieved record.

It works for me if I make the query between Test.startTest and Test.stopTest:

class DemoTest
    static testMethod void testLastViewedDate()
        Id recordId = SObjectFactory.create(Contact.sObjectType).Id;

            Contact record = [SELECT Id FROM Contact FOR VIEW];

        record = [SELECT LastViewedDate FROM Contact WHERE Id = :recordId];
        system.assertNotEquals(null, record.LastViewedDate);
  • I don't in the first query, but i do in the second? – Kasper Feb 16 '17 at 19:01
  • So if you made a third query, the LastViewedDate would be set. It doesn't get written into until you make such a query. The query first query to add FOR VIEW will not have had it set yet. @Kasper – Adrian Larson Feb 16 '17 at 19:02
  • Let me edit the question to make it a little clearer because it doesn't actually do that. Unfortunately the result for the third query is: |DEBUG|contact last viewed date after for view(Contact:{Id=003b000001bdyOAAAY}) – Kasper Feb 16 '17 at 19:03
  • @Kasper you need a stopTest call. – Adrian Larson Feb 16 '17 at 19:12
  • Thank you for your help. Fantastic that you are this quick! I have edited the question because i hadn't expressed myself clearly. Sorry for that. This issue doesn't occur in my test org. It only occurs in my production org. I get different results during validation/deployment of the incoming change set, than when the test class has been deployed. Just to be sure i added the start.test and stop.test but unfortunately that made no difference. Love to hear it if you have more ideas! – Kasper Feb 16 '17 at 19:39

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.