function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Zac RosenbergZac Rosenberg 

List.add( value || 'default value')?

Hello,

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!
Best Answer chosen by Zac Rosenberg
James LoghryJames Loghry
I think what you are looking for is more so the following:
temp.add((String.isEmpty(j.name) ? 'No Contact Name Associated' : j.name));

The above snippet uses a ternary operator that returns 'No Contact Name Associated' if Name is blank, otherwise the contact's name.  The output of the ternary is then added to the temp collection.

All Answers

KaranrajKaranraj
You can have IF condition to check value in the field, if not just put the default value
 
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){ 
            if(ja.Name != null)
            temp.add(j.name );
            else
             temp.add( 'No Contact Name Associated');  
            
            temp.add(j.id); 
        } 
    } 
    else{ 
        temp.add('No Contact Name Associated'); 
        temp.add('No Contact Id Associated'); 
    } 
};

Note: If record is created in salesforce, then it must contains Salesforce ID value. So for all records ID is not null value, if record is got created in salesforce and In contact object Name field is mandatory field, so it also won't contain null values for the records. 

 
James LoghryJames Loghry
I think what you are looking for is more so the following:
temp.add((String.isEmpty(j.name) ? 'No Contact Name Associated' : j.name));

The above snippet uses a ternary operator that returns 'No Contact Name Associated' if Name is blank, otherwise the contact's name.  The output of the ternary is then added to the temp collection.
This was selected as the best answer
Zac RosenbergZac Rosenberg
@Karanraj, you're code absolutely works, but I was hoping to keep my code as tight as possible. @James, your's does just that.

Thank you both for your responses!
Zac RosenbergZac Rosenberg
Also, Is it better to use String.isEmpty() or simple j.name == null? Is there a difference?
James LoghryJames Loghry
I use String.isEmpty of == null, because a String can be either null or empty string (''), and the isEmpty checks for both.
Zac RosenbergZac Rosenberg
aha. Thanks man!