7

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.

UPDATE 1

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

UPDATE 2

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 May 18 '14 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 May 18 '14 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 May 18 '14 at 18:15
  • @regal I'm not getting this - can you explain a bit more? – Keith C May 18 '14 at 21:16
3

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).

|improve this answer|||||
  • 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 Jun 1 '14 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. Nov 3 '14 at 17:02
0

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.

|improve this answer|||||

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.