2

After querying for Account sObjects, I'm adding all the fields into a List. The problem is that some Account sObjects have certain fields and others do not. While adding the fields to my list, I'd like to be able to add field if it exists OR add a default string.

Let's go through the example.

My query:

List<Account> accountQuery = [SELECT name, (SELECT name, id FROM Contacts) FROM Account];

My map:

Map<String, List<String>> Result = new Map<String, List<String>>();

Iterating through the queryResult and saving the values:

For(Account i : accountQuery){
    String[] temp = new list<string>();
    temp.add(i.name)
    //Now loop through contacts subquery
    if(!i.contacts.isEmpty()){
        for(contact j : i.Contacts){
            temp.add(j.name || 'No Contact Name Associated'); //Doesn't work
            temp.add(j.id || 'No Contact Id Associated');
        }
    }
    else{
        temp.add('No Contact Name Associated');
        temp.add('No Contact Id Associated');
    }
};

How can I achieve this temp.add(value || 'default value')?

Thank you!

1 Answer 1

4

Try using something like this

for(contact j : i.Contacts){
     string tempName = (j.Name == null) ? 'No Contact Name Associated' : j.Name;
     temp.add(tempName);
}

This is know as a ternary conditional statement.

From the docs.....

There is also a ternary conditional operation, which acts as short hand for an if-then-else statement. The syntax is as follows:

x ? y : z

and can be read as: if x, a Boolean, is true, then the result is y; otherwise it is z. Execute the following:

Boolean isIt = true; String x = 'You are ' + (isIt ? 'great' : 'small');

System.debug(x);

You can see the full doc here

EDIT

As pointed out by @sfdcfox, you can actually do this without the added step of an intermediary variable. This is what the code would look like

for(contact j : i.Contacts){
     temp.add((j.Name == null) ? 'No Contact Name Associated' : j.Name);
}
5
  • Thanks! This absolutely works. I guess there's no way to do this in a shorter, more concise way? I try to keep my code as minimal as possible. If we can't come up with anything tighter, I'll definitely mark this as correct! Thank you!
    – ZAR
    Feb 12, 2015 at 18:37
  • I don't think I can combine those two lines into one. This is as minimal as I can think of to make it. I definitely could be wrong and would like to see if anyone else has suggestions, but this is all I can think of. Feb 12, 2015 at 18:43
  • 2
    temp.add(j.Name == null? 'No Contact Name Associated' : j.Name). There's no need for an intermediary variable.
    – sfdcfox
    Feb 12, 2015 at 18:48
  • 3
    I actually have a function like that in a class: public static Object nullValue(Object a, object b) { return a == null? b: a; } This makes it clear that I want one value or the other, just like the function nullValue in Visualforce/formulas: temp.add((String)Data.nullValue(j.Name, 'No Contact Name Associated'));
    – sfdcfox
    Feb 12, 2015 at 18:50
  • Learn something new everyday. I actually thought to myself '@sfdcfox would know the definitive answer to this', while i was typing out my comment. Always love learning something new. Thanks alot! Feb 12, 2015 at 18:51

You must log in to answer this question.

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