+ Start a Discussion
Jim BoudreauxJim Boudreaux 

Wildcard in Apex

 

ok, I have a custom controller where I run a SOPL query and fill a list ir with the results.

Then I loop through ir and count up all the items according to result__c and device_type__c.

 

here's my code: 

 

public integer Devtotal(string dev, string res){ integer i = 0; for (inspection_result__c result : this.ir){ if ((result.device_type__c == dev)&&(result.result__c == res)){ i++; } } return i; }

 

So devtotal('smoke detector', 'passed') returns all the smoke detectors that passed, and devtotal('pull station', 'failed') returns all the pull stations that failed, and so on.

 

The problem is In my device_type__c field I have 'pull station' and 'smoke detector', but I also have 'pull station (double)',  'pull station (single)', 'smoke detector (ion)' and 'smoke detector (photo)'.

 

What happens right now is I get counts for each category, but what I want is counts for all the pull stations ('pull station' + 'pull station (double)' + 'pull station (single)') under pull station.

 

I could hard code this in, but if I ever add another option to my device_type__c picklist, I don't want to have to edit my controller.

 

What I want to be able to do is use a wildcard of some kind,  but I don't know if wildcards word here, in Apex as opposed to a SOQL query.

 

Something like this:

 

 

public integer Devtotal(string dev, string res){

dev = dev+'*';

integer i = 0; for (inspection_result__c result : this.ir){ if ((result.device_type__c like dev)&&(result.result__c == res)){ i++; } } return i; }

 

Obviously the above code won't work, so what would I do to make it work? 

 

Best Answer chosen by Admin (Salesforce Developers) 
XactiumBenXactiumBen

You could use:

 

 

if (result.device_type__c.startsWith(dev) && result.result__c == res)
...

 

 

 

All Answers

XactiumBenXactiumBen

You could use:

 

 

if (result.device_type__c.startsWith(dev) && result.result__c == res)
...

 

 

 

This was selected as the best answer
Jim BoudreauxJim Boudreaux
I ended up going with contains(dev), but yeah, startswith(dev) works too, thanks!