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
SolutionSolution 

Query and display Article ViewCount related to Article in Visualforce page

I have created one visualforce page which is showing complete list of Salesforce Knowledge Articles. Currently only Article Title field is shown on the page which I am quering from the standard object, KnowledgeArticleVersion.

 

I want to show one more field on the page which is TotalViewCount for a given article record. I am facing problem in impleting this fuctionality as the ViewCount field is there in another object called KnowledgeArticleViewSat.

 

Can someone help me? Thanks in advance

 

Thanks,

Pooja

Best Answer chosen by Admin (Salesforce Developers) 
GlynAGlynA

Pooja,

 

The KnowledgeArticleVersion has a field, KnowledgeArticleId, which identifies the article independent of version.  You should be able to use these Ids to query the KnowledgeArticleViewStat records that correspond to the articles.

 

The code below takes a list of KnowledgeArticleVersion records and returns a map from the KnowledgeArticleVersion ID to the view count for the article:

 

public static Map<Id,Integer> getKnowledgeArticleViews( List<KnowledgeArticleVersion> list_KAVs )
{
    Map<Id,Id> map_KAID_KAVID = new Map<Id,Id>();

    for ( KnowledgeArticleVersion kav : list_KAVs )
    {
        map_KAID_KAVID.put( kav.KnowledgeArticleId, kav.Id );
    }

    Map<Id,Integer> map_KAVID_ViewCount = new Map<Id,Integer>();

    for ( KnowledgeArticleViewStat kavs :
        [   SELECT  Id, ParentId, ViewCount
            FROM    KnowledgeArticleViewStat
            WHERE   ParentId IN :map_KAID_KAVID.keySet()
        ]
        )
    {
        map_KAVID_ViewCount.put( map_KAID_KAVID.get( kavs.ParentId ), kavs.ViewCount );
    }

    return map_KAVID_ViewCount;
}

 

You can use this map to populate a list of wrapper class instances that contain all of the information from the KnowledgeArticleVersion records and the view count.  Have your VF page repeat on this list of wrappers and you can display the view counts along with all the other information about the articles.

 

If you need help with the wrapper class, let me know.  I'll be happy to help.

 

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them.  Thanks!

 

-Glyn

 

All Answers

GlynAGlynA

Pooja,

 

The KnowledgeArticleVersion has a field, KnowledgeArticleId, which identifies the article independent of version.  You should be able to use these Ids to query the KnowledgeArticleViewStat records that correspond to the articles.

 

The code below takes a list of KnowledgeArticleVersion records and returns a map from the KnowledgeArticleVersion ID to the view count for the article:

 

public static Map<Id,Integer> getKnowledgeArticleViews( List<KnowledgeArticleVersion> list_KAVs )
{
    Map<Id,Id> map_KAID_KAVID = new Map<Id,Id>();

    for ( KnowledgeArticleVersion kav : list_KAVs )
    {
        map_KAID_KAVID.put( kav.KnowledgeArticleId, kav.Id );
    }

    Map<Id,Integer> map_KAVID_ViewCount = new Map<Id,Integer>();

    for ( KnowledgeArticleViewStat kavs :
        [   SELECT  Id, ParentId, ViewCount
            FROM    KnowledgeArticleViewStat
            WHERE   ParentId IN :map_KAID_KAVID.keySet()
        ]
        )
    {
        map_KAVID_ViewCount.put( map_KAID_KAVID.get( kavs.ParentId ), kavs.ViewCount );
    }

    return map_KAVID_ViewCount;
}

 

You can use this map to populate a list of wrapper class instances that contain all of the information from the KnowledgeArticleVersion records and the view count.  Have your VF page repeat on this list of wrappers and you can display the view counts along with all the other information about the articles.

 

If you need help with the wrapper class, let me know.  I'll be happy to help.

 

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them.  Thanks!

 

-Glyn

 

This was selected as the best answer
SolutionSolution

Thank you so much for your quick response. I have understood the concept of using the wrapper class. But I am new to wrapper class. Could you please help me with the code for wrapper class in this contextt?

 

It would be really helpful for me.

 

Thanks,

Pooja

SolutionSolution

I am able to display ArticleViewCount field on the visualforce page.

 

Thank you so much for the help.

 

Thanks,

Pooja


Solution wrote:

Thank you so much for your quick response. I have understood the concept of using the wrapper class. But I am new to wrapper class. Could you please help me with the code for wrapper class in this contextt?

 

It would be really helpful for me.

 

Thanks,

Pooja