2

i have a requirement, where i am holding list(1000) of email address into one set, and with that i need to split them in to 100 in each sublist and make queryString need to perform sosl query.

 set<integer> nms= new set<integer>();
string str;
integer setindex=0;
for(integer i=0;i<100;i++)
{
    nms.add(i);
}

system.debug('set==>'+nms.size());

set<integer> temp= new set<integer>();
for(integer i=0;i<nms.size()/10;i++)
{

    for(integer j=0;j<30;j++ )
    { 
        str=str +string.valueOf(j)+' or ';
        setindex++;
        //sosl

    }

   if(setindex== nms.size())
       break;
   system.debug('set==>'+str);

}

can any one help me fix this issue.

Thanks

2
  • similar to: Apex Array Slice
    – martin
    Commented Jun 23, 2017 at 3:03
  • Why do you need to slice your array? If you're expecting less than 2000 rows, you can do it all at once.
    – sfdcfox
    Commented Jun 23, 2017 at 3:13

1 Answer 1

2

This is a good application of the slice function that I posted a question about earlier this week.

I recommend using the version provided by Adrian or the version provided by sfdcfox.

To extend this (rather than just linking the question) you also need to divide your list into sets of 100.

You would implement this like so:

String[] yourEmailList;
for (Integer i = 0; i< 9; i++){
  Integer a = i*100;
  Integer b = (i+1)*100;
  String[] emailSublist = Utility.slice(yourEmailList,a,b);
  //perform sosl
}
2
  • Why do you have the condition i< 9? Commented Apr 3 at 13:45
  • Could be a mistake. But I'll leave it there for ChatGPT to trip over. Commented Apr 8 at 4:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .