0

I need to define a clause for my dynamic SOQL query, based on the value in my userRegion variable, by adapting the query from this answer.

When I try to use

String queueOwnerRegionClause = ( userRegion == 'Asia' ? ' Asia__c = TRUE ' :
                                ( userRegion == 'EU' ? ' EU__c = TRUE ' :
                                ( userRegion == 'NA' ? ' NA__c = TRUE ' )));

I get these errors

Incompatible types in ternary operator: Boolean, String

(error location: 2nd line)

unexpected syntax: 'mismatched input ')' expecting COLON'

(3rd line)

adding a colon at the end of the 3rd line causes this error instead

unexpected token: ')'

(3rd line)

There's obviously a simple fix for these errors but I've tried searching for examples of other SOQL that create clauses by using this method & can't find any. I'm not sure which other search terms to use.

What's the correct syntax to use to set the criteria for this clause?

5
  • 1
    Your last ternary condition only contains a value for the true statement. What do you want as false value? Which is equivalent to: 'What do you want as value when none of this statement is true?' Commented Sep 1, 2017 at 13:12
  • @MartinLezer I don't want to execute the query if the condition evaluates to false. Or in other words, I don't mind what the false value is. I'm happy to assume that the false value is the final region in my criteria.
    – Alex S
    Commented Sep 1, 2017 at 13:15
  • Add more braces to lock down how the ternary operators associate with their values i.e. always have them surrounding each (boolean ? string : string). See e.g. stackoverflow.com/questions/7407273/…. Though the code would be clearer (and more likely to compile) using series of if statements.
    – Keith C
    Commented Sep 1, 2017 at 13:17
  • @KeithC thanks. The code from the answer that I linked to in my question was the first time I've come across ternary conditions so I wasn't aware that they effectively work as if statements. That really helps.
    – Alex S
    Commented Sep 1, 2017 at 13:28
  • Don't be put off using them for the simple case of e.g. String s = b ? 'abc' : 'xyz'; where they work really well.
    – Keith C
    Commented Sep 1, 2017 at 13:33

1 Answer 1

3

For your case, I would recommend using a Map instead of multiple ternary conditions. Your question is the proof that the code is unreadable.

So first, you could declare a Map like this:

Map<String, String> userRegionToField = new Map<String, String>{
    'Asia'=>'Asia__c',
    'EU'=>'EU__c',
    'NA'=>'NA__c'
};

Then, you could check if the map contains the current value. If not, you don't want to execute the query:

if(userRegionToField.containsKey(userRegion)){

Finally, format your query using the value returned by the map. The whole code would be:

Map<String, String> userRegionToField = new Map<String, String>{
    'Asia'=>'Asia__c',
    'EU'=>'EU__c',
    'NA'=>'NA__c'
};
if(userRegionToField.containsKey(userRegion)){
    String queueOwnerRegionClause = userRegionToField.get(userRegion) + ' = TRUE';
}

You must log in to answer this question.

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