We've been struggling with an internal server error if the SOSL search text is
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:
- How is that possible? And how to guard against it in the future?
- 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%'
The NetworkId returned is the Salesforce Community ID.