10

I have a requirement, for each Accountwhose name contains ‘Sale’, ‘Opportunity’, or ‘Catalog’, set Price_Type__c to ‘Net’.

This is what I code in Anonymous Apex:

set<string> myString = new Set<String>{'%Sale%', '%Opportunity%', '%Catalog%'};
List<Pricebook2> myList = [select Name from Pricebook2 where name LIKE IN: myString];
for (Pricebook2 rec : myList) {
    rec.Is_Auto_Maintained__c  = true;
    rec.Price_Type__c ='Net;'
}
update myList;

and it threw an error : "

expecting a colon, found 'IN'

". So is there any other solution to this?

abdn
  • 525
  • 7
  • 20
Noob_NoVoice
  • 2,709
  • 16
  • 66
  • 88

2 Answers2

26

Somehow I found the solution :

String[] nameFilters = new String[]{'%Sale%', '%Opportunity%', '%Catalog%'};
List<Pricebook2> myList =
    [select Name
     from Pricebook2
     where name LIKE :nameFilters 
    ];
for (Pricebook2 rec : myList) {
        rec.Is_Auto_Maintained__c  = true;
        rec.Price_Type__c ='Net';
}
update myList;
Noob_NoVoice
  • 2,709
  • 16
  • 66
  • 88
4

You could solve this just by using some Or's.

string s1 = '%Sale%';
string s2 = '%Opportunity%';
string s3 = '%Catalog%';
List<Pricebook2> myList =
    [select Name
     from Pricebook2
     where Name LIKE : s1 Or Name Like : s2 Or Name Like : s3];
Chris Duncombe
  • 24,176
  • 11
  • 75
  • 116