We've been struggling with an internal server error if the SOSL search text is test:

List<List<Account>> accounts = [FIND 'test' IN ALL FIELDS 
    RETURNING Account(Name, Description, Phone, Type, Id 
    ORDER BY LastViewedDate DESC NULLS LAST, Name DESC)];

I could not reproduce it in other orgs, and no error occurs if I remove ORDER BY LastViewedDate DESC.

After some research, I came across known issues with LastViewedDate, so I started to suspect that field even more. But, then I decided to rule out the data, and sure enough I found duplicate records with the SAME Salesforce ID in our org. The image below shows the Id, Name, LastViewedDate, and CreatedDate fields:

duplicate IDs

Two questions:

  1. How is that possible? And how to guard against it in the future?
  2. Why does removing LastViewedDate help?

If it helps, we're using Salesforce Communities and Force.com Sites.


This is the SOQL query that revealed the duplicate IDs in Workbench. Developer Console shows two records instead of four, so that has me scratching my head too.

SELECT Id, Name, LastViewedDate, CreatedDate FROM Account


OK. It seems that the problem is in the RecentlyViewed records. Check out this query and its result:

SELECT Id,Name,LastViewedDate,NetworkId from RecentlyViewed WHERE Name LIKE '%Test%'

RecentlyViewed query results

The NetworkId returned is the Salesforce Community ID.

  • 1
    Can you include the SOQL query that produces the output you show in your image?
    – Keith C
    Commented May 18, 2014 at 17:40
  • Those aren't duplicate Id values, rows 100 and 101 are the same account and rows 102 & 103 are the same account - each with different Id values.
    – Mark Pond
    Commented May 18, 2014 at 17:41
  • 1
    If an account record is viewed 10 times in 1 minute you will get that record 10 times. Lastviewed date fetches all records with their unique view time. It doesn't check for duplicates. You are using SOSL thats matters.
    – Ashwani
    Commented May 18, 2014 at 18:15
  • @regal I'm not getting this - can you explain a bit more?
    – Keith C
    Commented May 18, 2014 at 21:16

2 Answers 2


I saw a conversation about this on twitter today and did some digging. It turns out there's a known issue, reference number W-203251, and is a high priority for the dev team to fix.

As of today there doesn't seem to be an estimated fix date however (although that doesn't imply it isn't being worked on, just that no fix rollout is scheduled yet).

  • 1
    Right on! Appears to affect records that have been viewed in multiple contexts. Both SOQL and SOSL have problems retrieving LastViewedDate for these records. SOQL incorrectly returns the record as two records (in some tools), and SOSL blows up.
    – geeljire
    Commented Jun 1, 2014 at 14:47
  • Do you have a link to the ticket? I'm getting the error but only with SOQL map syntax, with normal list syntax there's no problem. I.e. List<RecentlyViewed> rv = [Select Id, LastViewedDate from RecentlyViewed where Type = 'Contact' order by LastViewedDate desc limit 50]; works, but Map<Id, RecentlyViewed> rv = new Map<Id, RecentlyViewed>([Select Id, LastViewedDate from RecentlyViewed where Type = 'Contact' order by LastViewedDate desc limit 50]); fails
    – George S.
    Commented Nov 3, 2014 at 17:02

My pure speculation answer is that the RecentlyViewed table is queried and joined when you add LastViewedDate into your Account select statement. RecentlyViewed Object

If you require the LastViewedDate field, I would suggest using an aggregate query to pull that data by querying the RecentlyViewed object directly, and apply the results to a map based on some logic. If you want me to include a code sample, I'll edit this post.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .