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
mahimahi 

Error: Too many SOQL queries :21

Code:
trigger Ph1_calculatefieldLead on Lead (after update,after insert) {

//Lead [] le=Trigger.new;

Integer TotalPassToSales=0;
String LeadsViaCamapign='0125000000010WY';
String NonCampaginLeads='0125000000010WT';

for (Lead leLoop:Trigger.new){

 Integer CountCampaignPresent=[Select Count() from CampaignMember where LeadId=:leLoop.Id];
 
 //leLoop.adderror('e'+CountCampaignPresent);
 
 if(CountCampaignPresent==1){
 
  /* Select campaignid of linked Campaign */
  String LeadCampaignId=[Select CampaignId from CampaignMember where LeadId=:leLoop.Id].CampaignId;

  /* Select all LeadIds of Leads linked with a particular campaign */
  //CampaignMember [] LeadCampaignMember=[Select LeadId,CampaignId from CampaignMember where CampaignId=:LeadCampaignId];

  for(CampaignMember LeadCampaignMemberLoop:[Select LeadId,CampaignId from CampaignMember where CampaignId=:LeadCampaignId]){

   /* Check LeadId is not null */
   if(LeadCampaignMemberLoop.LeadId!=null){

    /* Select information about Lead linked with a particular Campaign*/
    Lead LeadCampaignLoop=[Select Status,lead_internal_lead_status_pck__c,RecordTypeid from Lead where id=:LeadCampaignMemberLoop.LeadId];

    /* Calculate Total Pass To Sales Leads */
    if(LeadCampaignLoop.Status=='Open - Passed to Sales' || LeadCampaignLoop.Status=='Closed - Accepted by Sales' || LeadCampaignLoop.Status=='Closed - Rejected by Sales' ){
     TotalPassToSales=TotalPassToSales+1;
     }
                }
}
/* Select campaign for Lead whose status is changed */
    Campaign camp=[select cam_tot_pass_to_sales_lead_num__c from campaign where id=:LeadCampaignId];

    /* Populating values into concerned fields of Campaign */
     camp.cam_tot_pass_to_sales_lead_num__c=TotalPassToSales;
               }
}

 hi all,
 
i tried to import 18 leads through data loader without any information about campaign.
i am getting error too many SOQL queries :21 at line ::::::Integer CountCampaignPresent=[Select Count() from CampaignMember where LeadId=:leLoop.Id] during after update . 
 
it is also saying 'data already exists' but no leads were present before importing.

please help me out.
 
regards,
mahi
Best Answer chosen by Admin (Salesforce Developers) 
venkat1venkat1
Force.com Sandbox

Alert: All information in the import file was already in Salesforce.

Result: No data was created or modified in Salesforce.

20 values: unexpected errors. Line number(s): 2, 3, 4, 5, 6, 7, 8, 9, 10

If you encounter any problems or have any questions, please contact us by clicking Help & Training at the top right of any Salesforce page and choosing the My Cases tab.

hi all,
 
  i got that error while importing data from CSV file to SalesForce CRM through ' Import Leads' Option (Administration Setup->Data Management->Import Leads.
Please give any suggestion for that error.
 
Thanks and Regards,
Venkatachalamoorthy C
 
 

All Answers

Cool_DevloperCool_Devloper

Dear Friends,

I am also facing this peculiar problem.

Does this mean that Triggers dont work well with Batch Load of data like though Apex Data Loader/Import Wizard?

Kindly Guide ....
 
Best Regards,
CD
steve_andersensteve_andersen
Look in the apex documentation for bulk triggers. It's bad to put soql statements in a for loop. the batch size for each trigger can be from 1 to 200. if the batch size is high, having a soql statement in the for loop will give you the soql error.

Steve
Cool_DevloperCool_Devloper
Thanks for the reply Steve....
 
I was using Import Wizard with some 18 records. In that also i got this error.
 
Actually, i need to have some processing on each record. Can u suggest how can i attain that  without having these SOQL errors?
 
Thanks so very much .....
 
Best Regards,
CD
steve_andersensteve_andersen
Look in the Apex documentation for dealing with bulk triggers. You can get the same thing done, you just have to go about it differently because of the SOQL limits.
Cool_DevloperCool_Devloper

Thanks Steve....

I understood your point. But we are new to this concept of Bulk Triggers Execution. I have gone through the Apex documentation. But was not able to relate well with our requirement.

Can you suggest some sample code apart from what is given in the Apex document?

Would be really helpful....

Thanks,

CD

Divya GoelDivya Goel
Do not put select query in for loop. Instead put all the ids on which you want fire select query in a SET
 
like

Set<ID> OppID = new Set<ID>();

for(opportunity o : Trigger.new)
{
OppID.add(o.id)
}
 
now fire the select query like
 
select ........ from ....... where Opportunity id in :OppID
 
Thanks
Divya Prakash Goel
Cool_DevloperCool_Devloper
Thanks Divya......
 
I read this method in the APEX documentation. But what if i want to process further on the results of the SELECT query you wrote in ur post?
 
For e.g i may need to find the leads attached to campaign whose id is stored in the Set as you specified. In this case, i would need to find the leads for each of these campaigns individually....
 
How can this be achieved ? Let me know if you want a further description of this .....
 
Thanks so much for your help......
 
Best Regards,
CD
mahimahi
Thanks Divya but requiremnet is somewhat different.
 
i have to update campaign field (number of leads having status 'xyz' ).
 
we can have information about lead and campaign linkage through CampaignMember only.
 
now suppose i update 100 leads with status 'abc' to 'xyz' and these are linked to 10 different Campaigns.
 
First i need to capture campaignid and leadid through CampaignMember.
 
after that taking each campaignid inside 'for' loop i need to capture leadids linked to that particular campaign.
 
after that checking status of all leads linked with that particular campaign, number of leads with status 'xyz' will be calculated.
 
then field in that campaign will be updated.
 
so i need to put SOQL in 'for' loop.
 
could you throw some light in this regard. it will be really appreciable....
 
thanks,
mahi
Divya GoelDivya Goel
You can select the compaign id while selecting leads
mahimahi
how do i differentiate which leads are linked to which particular campaign?
 
this is because i need to process some fields of each campaign individually.
Divya GoelDivya Goel

do not put sql inside for loop. Play with ids.

Do not put sql in loop neither update in for loop.  while updating create array of objects and then add modified objects in that array inside for loop. Then update the whole array outside the for loop.

Thanks

Cool_DevloperCool_Devloper
Yes .... we can select that. But than, if i need do some processing for each of these campaign ID, than how can i proceed without having to loop over this set??
venkat1venkat1
Force.com Sandbox

Alert: All information in the import file was already in Salesforce.

Result: No data was created or modified in Salesforce.

20 values: unexpected errors. Line number(s): 2, 3, 4, 5, 6, 7, 8, 9, 10

If you encounter any problems or have any questions, please contact us by clicking Help & Training at the top right of any Salesforce page and choosing the My Cases tab.

hi all,
 
  i got that error while importing data from CSV file to SalesForce CRM through ' Import Leads' Option (Administration Setup->Data Management->Import Leads.
Please give any suggestion for that error.
 
Thanks and Regards,
Venkatachalamoorthy C
 
 
This was selected as the best answer
Alan.MarcusAlan.Marcus
you are having errors in your trigger. turn off the trigger and see if it works?

I bet the error has to do with the batch size you are importing.

If the import file is greater than 20 records, batch settings in data loader.

If you are using import wizard, turn off the trigger temporarily.

I just had to deal with that email and this fixed it!

Best regards,

Alan Marcus