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
Hemant Rana 14Hemant Rana 14 

SOQL limit hitting when firing a query.

List<Campaign> campOBj=[Select id from campaign];
Set<Id> campId=new Set<Id>();
For(Campaign campValues : campOBj)
{
    campId.add(campValues.id);
}
For(Id campIdV : campId)
{
    List<campaignMember> countCM=[Select id from campaignMember where campaignid = : campIdV limit 30000];
}

This is a simple code but its giving error:
System.LimitException: Too many query rows: 50001

In my org their is total number of CAMPAIGN='66'
when is see in my DEBUG: it only work for 12 CAMPAIGN then gives this error:

Values in Debug Log-----
**campId701D0000000d0MQIAY
***count13516
**campId701D0000000cxAWIAY
***count10156
**campId701D0000000cylEIAQ
***count3833
**campId701D0000000cxXpIAI
***count14084
**campId701D0000000cxrfIAA
***count1
**campId701D0000000cwzFIAQ
***count3606

for rest of the 6 campaign its giving 0 count thats why i didnt mention here and after that for this campaign id its showing error and the debug stops-
**campId701D0000000cyZDIAY

what i was thinking when i added all the count its approx 46000 i think thats why the count is going more than 50,000 but its not possible can anyone give me a possible ans why this is giving error. as soql doesn't work like this.
 
sandeep sankhlasandeep sankhla
Hi

If you will use SOQL inside for loop then it will always hit the limit..As A best practise we should never use SOQL inside for loop..Avodi using it then you will not face the error..Let me knwo if you need any help on this

Thnaks
Sandeep
sandeep sankhlasandeep sankhla
Hi Hemant,

Insetead of for loop you can directly use set inside query..it will return you all camoaing where campaign id is inside that set..

List<Campaign> campOBj=[Select id from campaign];

Set<Id> campId=new Set<Id>();
For(Campaign campValues : campOBj)
{
    campId.add(campValues.id);
}

    List<campaignMember> countCM=[Select id from campaignMember where campaignid IN: campId limit 30000];

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
 
Hemant Rana 14Hemant Rana 14
List<Campaign> campOBj=[Select id from campaign];
Set<Id> campId=new Set<Id>();
For(Campaign campValues : campOBj)
{
    campId.add(campValues.id);
}
//For(Id campIdV : campId)
//{
    List<campaignMember> countCM=[Select id from campaignMember where campaignid in : campId limit 30000];
//}

Its still giving the same error. First i didn't used query inside for. I used like i mentioned now but when it gives the error then i decided to use query inside for as for every query it gets only one campaign id. And also in my scenario when the campaign are only 66 the query will not hit governor limit as query will run only for 66 times.
Ravi Kumar 259Ravi Kumar 259
Avoid SOQL and DML statements inside For loop ,Then we never get this error " System.LimitException "
Hemant Rana 14Hemant Rana 14
Ravi- its still giving error and their is no DML statements and neither any query in FOR loop.-
List<Campaign> campOBj=[Select id from campaign];
Set<Id> campId=new Set<Id>();
For(Campaign campValues : campOBj)
{
    campId.add(campValues.id);
}
List<campaignMember> countCM=[Select id from campaignMember where campaignid in : campId limit 30000];
ManojjenaManojjena
HI Hemant ,
This error is not comming from CampaignMember it is comming from Campaign .

List<Campaign> campOBj=[Select id from campaign]; From this line the error is throwing .
 please check in workbench the count of campain in your org .If posible add condition in your query .
As per your debug the (701D0000000cxXpIAI) is campaign id .

You can check the below link to query more than 50000 record 
https://developer.salesforce.com/forums?id=906F000000090EEIAY

Please let me know if you need any other help .