1

I have a piece of code:

if (testClone.testprocess__c !=null) {
                    if (testIteration.Key__c == 'ut') {
                        req.Status__c = 'testing';
                        testIteration.Status__c = 'active';
                    }
                }

Key__c is a field on a custom object and has certain values like 'ut', 'ml' etc. In the IF condition above I want to filter for all the different keys in the field and mark the status, meaning if (testIteration.Key__c == 'ut') || if (testIteration.Key__c == 'ml') & so on..

Can I use enum and add the keys to it, something like below:

public enum Key { UT, ML, RG, WB }
public static Map<Key, String> LABELS = new Map<Key, String>{
    Key.UT => 'ut',
    Key.ML => 'ml',
    Key.RG => 'rg',
    Key.WB => 'wb'
};

And then filter it in the IF condition above, if yes, how to fetch the enum keys ? Or How can I avoid adding multiple OR conditions to my IF above and still be able to achieve it?

2

Why don't you make a List and then see if the value of Key__c is contained in the List?

private final List<String> keys = new List<String>{'ut','ml','rg','wb'};

//some code

if (keys.contains(testIteration.Key__c)) {
    req.Status__c = 'testing';
    testIteration.Status__c = 'active';     
}
| improve this answer | |
  • how can I use something like Switch, like mentioned by Sergio, for a particular key and change the status. For List I dont see any method that works like switch. – user61184 Jun 23 at 15:27
  • @Sfdcnewuser Do you need to do something differently depending on each key? For instance do you need to set the status differently depending if the key is ut or ml or something else? – Arthlete Jun 23 at 15:33
  • yes that is what I intend to do @Arthlete – user61184 Jun 23 at 15:34
  • @Sfdcnewuser In this case you will have to go with a switch statement just as Sergio recommended. You will have to write down each when block for the switch separately. A List wouldn't be useful in that case. Is there something about his example that is unclear to you? – Arthlete Jun 23 at 15:44
  • 1
    I am trying to learn ways of using list and was wondering if I could do switching between list keys values for certain type of conditions. I think I got my answer! thank you. – user61184 Jun 23 at 15:49
1

I would take the approach of @Arthlete, but having a Set instead of a list, since the contains method, although documented, does not work properly por the List type.

Keep in mind that probably you do not need a map, since, if you have an ENUM like that one, you can get what you want by "key.UT.name()"

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_enum.htm#:~:text=An%20enum%20is%20an%20abstract,can%20define%20your%20own%20enum.&text=This%20method%20returns%20the%20values,of%20the%20same%20Enum%20type.

| improve this answer | |
  • I've never had a problem using the contains() method of the List class. What problems have you encountered with it? Can you share an article/source so I can read about it. – Arthlete Jun 23 at 8:23
  • There is this existing thread discussing about it:: salesforce.stackexchange.com/questions/96702/… Update: It seems that, at some point, they fixed all the issues that it presented, so nowdays it could be used indistincly with List or Set I guess – Gabriel Serrano Salas Jun 23 at 8:47
1

I'd recommend to have a switch statement

switch on (testIteration.Key__c) {
    when 'ut' {
        req.Status__c = 'testing';
        testIteration.Status__c = 'active';
    } when 'ml', 'rg' { // example with more than one value matching
        req.Status__c = 'xxx';
        testIteration.Status__c = 'active';
    }
    ...
    when else {
        // actions to take if it does not match any value
    }
}

If you decide to go with the enum approach, remember that you don't need to put Key. before the UT and so on, because apex is clever enough to know that you are "switching" on an enum and knows which enum is behind.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy