You need to sign in to do that
Don't have an account?

Repeat function to fill a list collection until collection size reaches 100 records
I'm using a couple of functions for filling a list of contacts. This is done by passing a list of custom BD__c object records to another function (retrieveTopOneHundred) which, in short, uses the BD__c objects to retrieve a list of contacts. Problem I'm trying to solve: I want to keep fetching contacts using the retrieveTopOneHundred function until the contactsListB list reaches 100 contacts. How can I rewrite the code below so that I can do this without having to write a bunch of nested "if(contactsListB.size() < 100)...find more contacts based on the next set of BD__c records"? I was hoping to use a Do While loop or something simple, but I just couldn't arrive at anything that worked. Thanks in advance for your help!
CODE:
public String TopOneHundred()
{
List < sObject > bd = StdSetControllerBD.getRecords();
List < Contact > contactsListB = retrieveTopOneHundred(bd);
if (contactsListB.size() < 100)
{
StdSetControllerBD.next(); //if we have less than 100 contact records then we will go get more by repeating the process, this time with the next set of bd records in the StdSetControllerBD
List < BD__c > nextRoundOfBdRecords = new List < BD__c > ();
for (sObject sob: StdSetControllerBD.getRecords())
{
BD__c sobd = (BD__c) sob;
nextRoundOfBdRecords.Add(sobd);
}
contactsListB.addAll(retrieveTopOneHundred(nextRoundOfBdRecords));
if (contactsListB.size() < 100)
{
StdSetControllerBD.next(); //if we have less than 100 contact records then we will go get more by repeating the process, this time with the next set of bd records in the StdSetControllerBD
List < BD__c > thirdRoundOfBdRecords = new List < BD__c > ();
for (sObject sob: StdSetControllerBD.getRecords())
{
BD__c sobdd = (BD__c) sob;
thirdRoundOfBdRecords.Add(sobdd);
}
contactsListB.addAll(retrieveTopOneHundred(nextRoundOfBdRecords));
}
}
String contactsJSON = JSON.serializePretty(contactsListB);
return contactsJSON;
}
CODE:
public String TopOneHundred()
{
List < sObject > bd = StdSetControllerBD.getRecords();
List < Contact > contactsListB = retrieveTopOneHundred(bd);
if (contactsListB.size() < 100)
{
StdSetControllerBD.next(); //if we have less than 100 contact records then we will go get more by repeating the process, this time with the next set of bd records in the StdSetControllerBD
List < BD__c > nextRoundOfBdRecords = new List < BD__c > ();
for (sObject sob: StdSetControllerBD.getRecords())
{
BD__c sobd = (BD__c) sob;
nextRoundOfBdRecords.Add(sobd);
}
contactsListB.addAll(retrieveTopOneHundred(nextRoundOfBdRecords));
if (contactsListB.size() < 100)
{
StdSetControllerBD.next(); //if we have less than 100 contact records then we will go get more by repeating the process, this time with the next set of bd records in the StdSetControllerBD
List < BD__c > thirdRoundOfBdRecords = new List < BD__c > ();
for (sObject sob: StdSetControllerBD.getRecords())
{
BD__c sobdd = (BD__c) sob;
thirdRoundOfBdRecords.Add(sobdd);
}
contactsListB.addAll(retrieveTopOneHundred(nextRoundOfBdRecords));
}
}
String contactsJSON = JSON.serializePretty(contactsListB);
return contactsJSON;
}
Have you tried using a while loop as below:
Please let me know if this worked.
Thanks,
Shyama
All Answers
Have you tried using a while loop as below:
Please let me know if this worked.
Thanks,
Shyama