-3

I have a code where in there is a for loop and inside for loop I have a SOQL with 'LIKE' operator,

its like this

 for()
{
schemes=[SELECT id, name from somecustom object LIKE :name ]
} 

and name is being evaluted everytime for each iteration over for loop

I need to pull soql out of for loop How do i achieve this? Any ideas would be of great help

2
  • I need to fetch schemes with a soql and not boolean,so I don't think this helps May 24, 2021 at 13:38
  • Your post uses pseudocode which is a bit too lacking in detail. What kind of wild card patterns are you using? Do you expect the Name to start with your filter value, contain it, end with it, etc?
    – Adrian Larson
    May 24, 2021 at 13:45

1 Answer 1

3

What you can do is, inside your first for loop you can get all the name in one set of String and then you can fetch all the data from your custom object where name matches name(s) in your set

Below is the code example:

set<String> names=new set<String>();
for(CustomObject obj : [SELECT Id,Name from CustomObject WHERE yourfilter LIMIT 49999]){
names.add(obj.Name); //example names.add('%'+obj.Name+'%') or names.add(obj.Name+'%')
}
list<somecustomobject> schemes=[SELECT id, name from somecustomobject WHERE Name LIKE  :names LIMIT 49999];

Not the answer you're looking for? Browse other questions tagged or ask your own question.