+ Start a Discussion
ArchieTechie6ArchieTechie6 

SOQL query results - Binding to object returns only one record

Hi All,
I have a scenario where I need to execute a SOQL query inside of "For" loop.
When I execute this, the query returns 4 results. But, the object "PendingApprovals" contains only last record. 
 
public list<Knowledge__kav> PendingApprovals {get;set;}
string kbid;                    
             for(Knowledge__kav KnwObj:knowledgeList)
             {
                 
                  if(setOfPendingArticle.contains(KnwObj.Id))
                  {
                      
                      kbid= KnwObj.Id;
                                     
                      PendingApprovals = [Select Id ,Title, 
                      Summary__c, LastModifiedDate , 
                                                     CreatedById ,PublishStatus                         
                                                       from Knowledge__kav 
                                                     where Id  = :kbId];
                                                             
                  }
           }
            
I have 4 values of kbId. So, PendingApprovals should contain 4 items. But, it returns only one(last value). 
How do i loop through 4 values of kbid? 

Also, please suggest if SOQL query can be executed inside of for loop like I am doing. 
Any other better way to achieve this? 

Thanks!
Best Answer chosen by ArchieTechie6
Nikhil Verma 6Nikhil Verma 6
You should never use a SOQL inside a for loop. There is a limit of a maximum of 100 SOQL in a single execution. You can utilise the collection items (Sets, Lists, etc.) to achieve the result. You may use the following code:

    public list<Knowledge__kav> PendingApprovals {get;set;}
    Set<string> setkbid = new Set<String>();                    
    for(Knowledge__kav KnwObj:knowledgeList)
    {
        if(setOfPendingArticle.contains(KnwObj.Id))
        {
            setkbid.add(KnwObj.Id);
        }                                                    
    }
    PendingApprovals = [SELECT Id, Title, 
                          Summary__c, LastModifiedDate, 
                          CreatedById ,PublishStatus                         
                          FROM Knowledge__kav 
                          WHERE Id IN : setkbid];   

Hope this helps.

All Answers

Nikhil Verma 6Nikhil Verma 6
You should never use a SOQL inside a for loop. There is a limit of a maximum of 100 SOQL in a single execution. You can utilise the collection items (Sets, Lists, etc.) to achieve the result. You may use the following code:

    public list<Knowledge__kav> PendingApprovals {get;set;}
    Set<string> setkbid = new Set<String>();                    
    for(Knowledge__kav KnwObj:knowledgeList)
    {
        if(setOfPendingArticle.contains(KnwObj.Id))
        {
            setkbid.add(KnwObj.Id);
        }                                                    
    }
    PendingApprovals = [SELECT Id, Title, 
                          Summary__c, LastModifiedDate, 
                          CreatedById ,PublishStatus                         
                          FROM Knowledge__kav 
                          WHERE Id IN : setkbid];   

Hope this helps.
This was selected as the best answer
ArchieTechie6ArchieTechie6
This really helps. I read about Governance Limits but thought its applicable only for Triggers and in Update Scenarios but I thought its not applicable in "Select" queries. Thank you for the explanation!