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
Amit Singh1989Amit Singh1989 

System.LimitException:Too many SOQL queries: 101

Hello,

 

I have two custom objects job and Campaign with Lookup relationship. (Campaign is Master and Job is Child)

 

i want to display list of Campaign with its child records in a visualforce page , and i am using this code -:

 

public with sharing class JobTest1

{

    // Global variables

     public String JName {get; set;}

   

     // Start Constructor

     public JobTest1()

     {

         today=System.Today();

     }

     // End of Constructor

 

    public class cNodes

    {

         public List<Job__c> parent {get; set;}

         Public Campaign__c gparent {get;set;}

         public cNodes(Campaign__c  gp, List<Job__c> p)

         {

             parent = p;

             gparent = gp;

         }

    }

 

    Public List<cNodes> hierarchy;

 

    Public List<cNodes> getmainnodes()

    {

        hierarchy = new List<cNodes>();

        List<Campaign__c> tempparent = [Select Id,Name from Campaign__c LIMIT 10000];

        for (Integer i =0; i< tempparent.size() ; i++)

        {

               List<Job__c> tempchildren = [Select Id,Name,Job_Id__c,Client__c,Client__r.Name,Campaign__c,Completion_Date__c,Start_Date__c,Due_Date__c from Job__c where Campaign__c = :tempparent[i].Id Order By Name];

               hierarchy.add(new cNodes(tempparent[i],tempchildren));

        }  

        return hierarchy;

    }  

}

 It works if there is less record,but if records are huge it throws following error -:

 

 System.LimitException: JobSuite:Too many SOQL queries: 101

How can we fix this issue.

 

Thanks

 

 

  

 

Navatar_DbSupNavatar_DbSup

Hi,

 

You are getting this error because you have used SOQL inside for loop.  You have to remove the SOQL from the loop.

 

Try the below code as reference:

 

Public List<cNodes> getmainnodes()

    {

 

        hierarchy = new List<cNodes>();

 

        List<Campaign__c> tempparent = [Select Id,Name from Campaign__c LIMIT 10000];

  list<id> ids=new list<id>();

        for (Integer i =0; i< tempparent.size() ; i++)

 

        {

  ids.add(tempparent[i].id);

  }

 

        for(Job__c tempchildren : [Select Id,Name,Job_Id__c,Client__c,Client__r.Name,Campaign__c,Completion_Date__c,Start_Date__c,Due_Date__c from Job__c where Campaign__c in :ids Order By Name];

   {

               hierarchy.add(new cNodes(tempparent,tempchildren));// Make changes accordingly.

   }

 

        return hierarchy;

 

    }

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

sunny.kapoor1306sunny.kapoor1306

This is due to apex governor limits

Amit Singh1989Amit Singh1989

Hi Navatar_DbSup ,

 

I am getting following error with the help of your code,

 

"Error: Compile Error: Constructor not defined: [JobLogSheetTest1.cNodes].<Constructor>(LIST<Campaign__c>, SOBJECT:Job__c) at line 58 column 23".

 

this is the line no. 58 :  hierarchy.add(new cNodes(tempparent,tempchildren));// Make changes accordingly.

 

it seems error in this code :

public class cNodes
    {
         public List<Job__c> parent {get; set;}
         Public Campaign__c gparent {get;set;}
         public cNodes(Campaign__c  gp, List<Job__c> p)
         {
             parent = p;
             gparent = gp;
         }
    } 

 

here List of Job and campaign is used but at line 58, it returns list of Campign , i have corrected it as,

 public class cNodes
    {
         public Job__c parent {get; set;}
         Public List<Campaign__c> gparent {get;set;}
         public cNodes(List<Campaign__c>  gp, Job__c p)
         {
             parent = p;
             gparent = gp;
         }

    }

 

but it seems incorrect because i want to display list of Campaign with its jobs in a list view.

for example -:

 

Camp 1

    Job 1

    job 2

    Job 3

 

Camp 2

  Job 4

  job 5

  job 6

  

.

..

.

. etc

 

Thanks