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
CevicheroCevichero 

How to "Bulkify" this method?

I am currently hitting the limit for 100 SOQL queries. I have a method that takes a case ID and returns a boolean to indicate if the case has a public article attached or not. The problem is that I cannot see the way to have this method work in bulk, that meaning, taking a list or a set of case ids and returning a map of caseis and booleans. 

 

This is the code currently working for a single case (or for a small number of cases). I want to be able to call the getPubliArticles method like this:

 

 

map<Id, Boolean> caseArticlesmap = getPublicArticles(caseids); //so that then I can iterate over this map.

 

These are the methods tha I am currently using.

 

 

private List<KnowledgeArticleVersion> loadArticlesAttachedToCase(){
        List<KnowledgeArticleVersion> attachedArticles = new List<KnowledgeArticleVersion>();
       
        List<CaseArticle> caseAssociations = [
            select KnowledgeArticleId, ArticleLanguage
            from CaseArticle
            where CaseId =:CaseId];
       
        if (caseAssociations != null && caseAssociations.size() > 0){
            
            
            for (CaseArticle association : caseAssociations){
                String strKnowledgeArticleId = association.KnowledgeArticleId;
                String strArticleLanguage = association.ArticleLanguage;

                String aQry = 'select id, Title, KnowledgeArticleId, Language, UrlName '
                            + 'from KnowledgeArticleVersion '
                            + 'where KnowledgeArticleId = :strKnowledgeArticleId '
                            + 'and PublishStatus = \'Online\' '
                            + 'and Language = :strArticleLanguage '
                            + 'and IsVisibleInPKB = true '
                            + 'order by KnowledgeArticleId';
                            
                List<KnowledgeArticleVersion> kav = Database.query(aQry);
                if (kav.size() > 0) {
                    attachedArticles.add(kav[0]);
                }
            }
        }
        
        return attachedArticles;
    }

 

 

 

Thanks in advance! Any help will be greatly appreciated.

sivaextsivaext

Hi

 

Salesforce best practices : don't write soql queries inside for loop but you had written soql query inside for loop. 

 

http://wiki.developerforce.com/page/Apex_Code_Best_Practices

 

 

CevicheroCevichero

I was able to get this working.