+ Start a Discussion
Chiel de Graaf.Chiel de Graaf. 

Using list custom setting in APEX

Hi all,

We have a custom setting which we use to display the top 5 most viewed articles on a VF page.
The APEX code is written so to retrieve those articles, but when i add a 6th entry and set the limit to display 6 articles, the first 5 are properly displayed, but the 6th is the first article in our KB instead of the article marked as number six in the custom setting.

I tried to figure out why this happens, but i can't find the problem.
This is the part that was written in the controller Apex.

Does anyone has an idea on how to properly display the 6th article ?
//Get most top 5 viewed articles
    private void getTop5Article(){
        final Integer TOP5_SIZE_LIMIT = 6;
        
        numAllDtoResult = 0;
        isDisableGotoContact = true;
        searchType = '';
        lDTOResult = new List<DTOResult>();
        
        List<sObject> lArticleWithTop5 = KBArticleUtil.getArticleByTopFiveOrder(KBArticleUtil.ARTICLE_OBJECT_NAME);
        Map<String, String> mVoteTmp = KBArticleUtil.getMapVote(lArticleWithTop5);
        
        for(sObject obj:lArticleWithTop5){
            Aurora__kav k = (Aurora__kav)obj;
            for(Aurora__DataCategorySelection dcs:k.DataCategorySelections){
                if(dcs.DataCategoryGroupName==ClsCategory.DEFAULT_ROOT_CATEGORY){
                    lDTOResult.add(new DTOResult(k, mAllCategorySuggestion.get(dcs.DataCategoryName), '', mVoteTmp.get(k.KnowledgeArticleId)==null?false:true, mVoteTmp.get(k.KnowledgeArticleId)));
                    break;
                } 
                
            }
            if(lDTOResult.size()==TOP5_SIZE_LIMIT) break;
        }
        
        if(lDTOResult.size()<TOP5_SIZE_LIMIT){
            List<sObject> lArticleOrderByTitle = KBArticleUtil.getArticleOrderByTitle(KBArticleUtil.ARTICLE_OBJECT_NAME);
            for(sObject obj1:lArticleOrderByTitle){
                Aurora__kav k1 = (Aurora__kav)obj1;
                for(Aurora__DataCategorySelection dcs:k1.DataCategorySelections){
                    if(dcs.DataCategoryGroupName==ClsCategory.DEFAULT_ROOT_CATEGORY){
                        lDTOResult.add(new DTOResult(k1, mAllCategorySuggestion.get(dcs.DataCategoryName), '', mVoteTmp.get(k1.KnowledgeArticleId)==null?false:true, mVoteTmp.get(k1.KnowledgeArticleId)));
                        break;
                    } 
                }
                
                if(lDTOResult.size()==TOP5_SIZE_LIMIT) break;
            }
        }
    }
    
    //Initialize all categories
    public void initializeCategories(){
        ClsCategory.initCategories();
        if(!ClsCategory.isError){
            lLevelOneCate = ClsCategory.lCateLvOne;
            mLevelTwoCate = ClsCategory.mCateLvTwo;
            mLevelThreeCate = ClsCategory.mCateLvThree;
            mAllCategorySuggestion = ClsCategory.mAllCateLv;
        }else{
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, ClsCategory.errorMsg));
        }
    }

 
James LoghryJames Loghry
It's hard to follow this code, especially not knowing how the getMapVote method, etc works exactly.  However, this should be pretty straight forward.  If you're custom setting doesn't have an "order" field, I would add one, so that you can dictate the exact order the articles come in.

Next, you iterate through the custom settings (using SOQL to query the custom settings by that order field).  Then you iterate through the articles, and if the Id or matching criteira are met, then you add them to your result set.