0

I have a line of code in my test class:

List<sObject> newRecentlyViewed = 
      Test.loadData(RecentlyViewed.sObjectType, 'MyRecentlyViewedData');

The csv I uploaded to the static resource included: Id, Type, Name

When running the test I get the error message: System.SObjectException: Required fields are missing: [OwnerType]

When I look at the RecentlyViewed object in the query editor there is no field "OwnerType".

The class is finding recently viewed Knowledge Articles. But I am having a hard time figuring out how to test for recently viewed Knowledge Articles by inserting test data and querying for that.

Any help?

5
  • 1
    The RecentlyViewed object doesn’t support the Event, Task, Report, KnowledgeArticle, and Article objects. from the object ref doc
    – cropredy
    Commented Dec 16, 2022 at 0:39
  • It looks like the original code for the class had the search as: SELECT Id, Type, Name FROM RecentlyViewed WHERE LastViewedDate !=null ORDER BY LastViewedDate DESC LIMIT 20 Then looked at the value of the Id and if it started with 'k' it was defined as a KnowledgeArticleVersion record. Would that work, or would it have at one time? Commented Dec 16, 2022 at 1:20
  • that would have been fragile; there's better ways of detecting an sobjectType from an Id - see Id class' method getSObjectType(). Others in the Community may have better insight on KAVs and recentlyViewed - are you looking to test code that previously didn't have a testmethod?
    – cropredy
    Commented Dec 16, 2022 at 18:17
  • @cropredy yup. you got it. The problem is, as you say RecentlyViewed doesn't support KnowledgeArticle so even if they were trying to grab the Id of the RecentlyViewed record, no KnowledgeArticle would ever show up in this list. Which makes me wonder if it ever actually worked. Hopefully I can just recommend to scrap the whole thing because it seems useless. Commented Dec 16, 2022 at 21:59
  • well -- if it never worked, it is tech debt so I support the notion of killing it
    – cropredy
    Commented Dec 17, 2022 at 1:05

1 Answer 1

1

The RecentlyViewed object doesn’t support the Event, Task, Report, KnowledgeArticle, and Article objects. From the Object Ref doc.

I'd question whether the underlying code-under-test ever worked.

If you can't scrap it and have to jam in a testmethod, you can mock KAVs using an Sobject Fabricator factory such as my personal favorite on GitHub by Matt Addy

You must log in to answer this question.

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