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
Andrew GAndrew G 

how to fix bad RecentlyViewed records

Ok,  here's a doozy, because Salesforce Support said come here.

OK, SOQL query using MAP
Map<Id, RecentlyViewed> recentlyViewedMap = new Map<Id, RecentlyViewed>([SELECT Id FROM RecentlyViewed WHERE Type = 'Account']);
It throws an error:
Line: 1, Column: 1
System.ListException: Row with duplicate Id at index: 19


So, i then change up the query and right it as below with System.debug to analsye the query results:
List <RecentlyViewed> recentlyViewedList = new List <RecentlyViewed> ([SELECT Id, Name FROM RecentlyViewed WHERE Type = 'Account']);

for(RecentlyViewed a : recentlyViewedList ) {
    System.debug( 'a' + a.Id + ':' + a.Name);
}
When i review the debug, I can see that the records contain a duplicate.  When I viewed the Recently Viewed Accounts via the UI, the duplicate record does not display.  The duplicate only appears when I complete the SOQL and pass to a LIST.

So, how do I clear the duplicated records from what I assume is the RecentlyViewed table that sits in the back end of Salesforce?

Regards 
Andrew

notes:
1.  I know why the error is being thrown.  
2.  Yes, I know how to write code to side step the issue, but why should I need to code to cope with Salesforce having corrupted tables?
3. I have tried clearing Cache in my browser and even tried a different browser, but when we think about this, the issue is not how it is being displayed in a browser, but how the results are being pulled from a SF table (for want of a better term).
4.  The code (i.e. the SOQL query to MAP) does not throw an error for 3 other people I had test this.  The error pertains to my recently viewed records only.

 
Best Answer chosen by Andrew G
Andrew GAndrew G
Here is the solution.

It appears the cause of they issue is viewing a record both internally and via the Community interface.   

The RecentlyViewed table has a column called "NetworkId" which only becomes available when Communities are enabled.  The field records the "Community" that was the context when viewing the record.  This would, I guess, give the user the ability to have different recently viewed record results when using various Community portals on the one Sf platform.

The solution is to add a filter to your SOQL query that factors in the NetworkId.  When viewed internally, no NetworkId is recorded, so the following will work:
Map<Id, RecentlyViewed> recentlyViewedMap = new Map<Id, RecentlyViewed>([SELECT Id FROM RecentlyViewed WHERE Type = 'Account' AND NetworkId ='']);
Now, in my case, I need to now modify a Utility Class to cope with this update.

Once I had the feedback from Salesforce, I then managed to find this Stack Exchange article which also discusses the issue.  
https://salesforce.stackexchange.com/questions/36372/duplicate-ids-in-sandbox-how-come


@abhishek
you were dang close with your ProfileId reference.


Hope the above helps out someone else

regards
Andrew

 

All Answers

Abhishek BansalAbhishek Bansal
Hi Andrew,

It might be possible that the same record is viewed by two users at the same time so this table will have the two records differntiated by the Profile ID. SO in order to void the duplicate records please mention the profile id in the query.

Thanks,
Abhishek Bansal
VinayVinay (Salesforce Developers) 
Hi Andrew,

You are right RecentlyViewed is something that cannot be controlled from UI.

Review below Idea link and vote for this feature.

https://trailblazer.salesforce.com/ideaView?id=08730000000jvNbAAI

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar
Andrew GAndrew G
Hi @Vinay
Yep, found that idea and have upvoted and posted there previously.

@Abhishek 
Interesting idea.  However, if we think about how RecentlyViewed table works, surely it is already limited to my user profile, otherwise when I view my recently viewed records, i would be seeing everyone's recently viewed records in the Org.  Additionally, everyone would be getting the same error that I do, however, my testing shows that the corruption appears to be only in my "RecentlyViewed" Accounts. 

But, out of curiousity, I ran the query in the developer console and found that the Profile Id appears to be only populated against recenty viewed User records.  The Account records have no entry for profile Id.

Thanks both for having a look.

Regards
Andrew
Andrew GAndrew G
Here is the solution.

It appears the cause of they issue is viewing a record both internally and via the Community interface.   

The RecentlyViewed table has a column called "NetworkId" which only becomes available when Communities are enabled.  The field records the "Community" that was the context when viewing the record.  This would, I guess, give the user the ability to have different recently viewed record results when using various Community portals on the one Sf platform.

The solution is to add a filter to your SOQL query that factors in the NetworkId.  When viewed internally, no NetworkId is recorded, so the following will work:
Map<Id, RecentlyViewed> recentlyViewedMap = new Map<Id, RecentlyViewed>([SELECT Id FROM RecentlyViewed WHERE Type = 'Account' AND NetworkId ='']);
Now, in my case, I need to now modify a Utility Class to cope with this update.

Once I had the feedback from Salesforce, I then managed to find this Stack Exchange article which also discusses the issue.  
https://salesforce.stackexchange.com/questions/36372/duplicate-ids-in-sandbox-how-come


@abhishek
you were dang close with your ProfileId reference.


Hope the above helps out someone else

regards
Andrew

 
This was selected as the best answer