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
VFVF 

collection exceeds maximum size: 1001

Hi ,

Below is the class which i am using to retrieve leadis and contactids from campaign which are a total of more then 2000 records from select query using for loop.

 

public class Test

{

   Set<Id> leadid = new Set<Id>();

        Set<Id> contactid = new Set<Id>();

        List<String> emails=new List<String>();

 

public void TestMethod()

{

 

  for(campaignmember camp:[Select leadid,contactid from campaignmember where campaignid=:'campaignId']) {

  leadid.add(camp.leadid);

contactid.add(camp.contactid); 

 

 

 

 

here i am able to run the query which has more than 2000 records in the for loop but unable to add the leadid and contactid to the List<Id> (i.e  leadid , contactid) respectively as shown above.

An exception - collection exceeds maximum size: 1001 is being disaplayed.

 

My actual intenstion is to get the leadids and contactids and run the respective select query and add the email address to a list string emails.

 for (Contact cnt: [Select Email from contact WHERE Id IN :contactid]) 

 {

         if(cnt.Email != null)

         {

              emails.add(cnt.Email);

         }

 } 

 

can any one help me out.Thanks in advance

 

Thanks 

prashanth. 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
IanRIanR

Hi,

You need to split it out into batches of 1000, due to the array size limit, like

 

 

for (CampaignMember camp : [SELECT leadId, contactId FROM CampaignMember WHERE campaignId =: campaignId]) { // Add the campaignMember's lead Id to the Set. leadId.add(camp.leadId); // If we have filled the set, process it and empty it if (leadId.size() > 999) { ProcessLeads(leadId); leadId.clear(); } // Now repeat for the Contact Id... contactId.add(camp.contactId); if (contactId.size() > 999) { ProcessContacts(contactId); contactId.clear(); }}// Don't forget to handle what's left in the arraysProcessLeads(leadId);ProcessContacts(contactId);

 

What work you put into the methods ProcessLeads and ProcessContacts is up to you of course...

 

 

 

HTH,

 

Ian Randall 

 

 

All Answers

IanRIanR

Hi,

You need to split it out into batches of 1000, due to the array size limit, like

 

 

for (CampaignMember camp : [SELECT leadId, contactId FROM CampaignMember WHERE campaignId =: campaignId]) { // Add the campaignMember's lead Id to the Set. leadId.add(camp.leadId); // If we have filled the set, process it and empty it if (leadId.size() > 999) { ProcessLeads(leadId); leadId.clear(); } // Now repeat for the Contact Id... contactId.add(camp.contactId); if (contactId.size() > 999) { ProcessContacts(contactId); contactId.clear(); }}// Don't forget to handle what's left in the arraysProcessLeads(leadId);ProcessContacts(contactId);

 

What work you put into the methods ProcessLeads and ProcessContacts is up to you of course...

 

 

 

HTH,

 

Ian Randall 

 

 

This was selected as the best answer
VFVF

Heyy,

Thanks for your reply ...i was able to solve the issue..

Thanku very much... 

Message Edited by VF on 09-10-2009 07:15 AM