+ Start a Discussion

How to compare elements in a list?

I have a list with multiple elements associated with one name. I want to check only if the email address in the first element is empty - the others I don't care if they are blank. Here is my list:


public List<Opportunity> opportunity {
        get {
            return [SELECT Name, Ashoka_Website_Profile_Link__c,
            (SELECT Engagement_Role__c.Name,
            FROM Opportunity.R00NR0000000UWbWMAW WHERE Role__c='Nominator' AND Engagement_Role__c.Contact__r.Email != NULL ) 
            FROM Opportunity WHERE Id = :oppId];


Originally, I did not use a list but instead just created a new Opportunity object. My code all ran fine. I used this if-statement and for-loop after (pardon the terrible naming, it was not my doing):


        if (opportunity.R00NR0000000UWbWMAW[0].Contact__r.Email != null) {
            addresses = opportunity.R00NR0000000UWbWMAW[0].Contact__r.Email;
            for (Integer i = 1; i < opportunity.R00NR0000000UWbWMAW__r.size(); i++) {
                if (opportunity.R00NR0000000UWbWMAW[i].Contact__r.Email != null) {          
                    addresses += ':' + opportunity.R00NR0000000UWbWMAW[i].Contact__r.Email;


But since replacing my code with the list, my code doesn't work anymore. How then would the syntax look? I suppose first I would need to check if the list is empty in the first place, but after that, how would I check if the email field specifically is empty?

If(opportunity.isEmpty) OR

If(opportunity == Null || opportunity == '')

Thank you for your response. But is there a way to specifically to check if the email field is null? Because any of the other fields in my Opportunity can be null, except for the email.

I am not sure ,if you have email field in Opportunity . But ,as per the above list<opportunity> ,could you please try to add( list of oppty) related email in separate list<string> opp_email . Once you do it , then you make if (Opp_email.isempty) ;