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
Jim BoudreauxJim Boudreaux 

System.Exception: Too many SOQL queries: 101

Looking for some general help with this issue.

 

Also, in my efforts to limit my SOQL queries, I am trying to combine redundant ones using relationship queries. Such as

Select (Select Fire_Alarm_Inspection__c, Fire_Protection__c, Fire_Extinguisher__c, Fire_Suppression__c From Inspections__r), (Select Name__c From Floors1__r where exits_to_ground__c = true) From Account_c__c a Where ID = 'a0A70000006jSDV'

 

 My problem lies in how exactly to access the dat returned in this query. I tried this:

 

 

Account_c__c account =[Select (Select Fire_Alarm_Inspection__c, Fire_Protection__c, Fire_Extinguisher__c, Fire_Suppression__c From Inspections__r), (Select Name__c From Floors1__r where exits_to_ground__c = true) From Account_c__c a Where ID = :buildingid];ground = account.Floors1__r.name__c;

 but I am being told "Invalid foreign key relationship: Account_c__c.Floors1__r ", I ran the above query in the schema browser in Eclipse and got the results I wanted, but it won't work in my apex, or if it is working, I don't know how to access the data.

 

Thanks in advance for the help. 


 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
HarmpieHarmpie

There's definitely limits to what you can build on the platform due to all `govern limits, but still you can create pretty advanced stuff.

 

To avoid hitting limits, 'bulkify' your code. A good article about it can be found here:http://wiki.developerforce.com/index.php/Apex_Code_Best_Practices

Message Edited by Harmpie on 07-22-2009 11:06 AM

All Answers

HarmpieHarmpie

Looking at your queries, I would expect account.Floors1__r to contains a List of Floors1__c records (all related Floors1__c records to the Account you queried). So basically you will need a loop to access them:

 

 

for(Floors1__c floor : Floors1__r) {
ground = floor.Name__c;
}

 

Not 100% sure if i got object names and relations right (you sure chose some weird names for the objects and fields), but this is what the posted query suggests.

 

 

Message Edited by Harmpie on 07-22-2009 05:33 AM
HarmpieHarmpie
On a side-note: any object queried as a subquery/nested query, will also count as 1 query towards the limit.
Jim BoudreauxJim Boudreaux

I sure do! In retrospect Account_c__c was a horrible choice. LOL

 

Anyway, I see what you mean with the results being a List, but would I have to cycle through like you said even if the query only returns one result? 

HarmpieHarmpie

ground = Floors1__r[0].Name__c;

 

 

would suffice then.

Jim BoudreauxJim Boudreaux

OK, so here is a SOQL limit question, if I write an Apex Class that uses, say 50 SOQL queries and then I write a Controller that calls that Class three times, will I run against the 101 SOQL Query limit?

 

Probably, huh?

 

If so, how does one write complicated, scaleable Apex code while staying within the 101 allowable SOQL queries?

HarmpieHarmpie

There's definitely limits to what you can build on the platform due to all `govern limits, but still you can create pretty advanced stuff.

 

To avoid hitting limits, 'bulkify' your code. A good article about it can be found here:http://wiki.developerforce.com/index.php/Apex_Code_Best_Practices

Message Edited by Harmpie on 07-22-2009 11:06 AM
This was selected as the best answer