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
Glen.ax1034Glen.ax1034 

email.toaddresses.contains

email.toAddresses.contains('teameast@2p0b7hdjfv8olr8hubm9unhg.37creai.7.case.salesforce.com'))

 

Save Error: Method does not exist or incorrect signature: [List <String>].contains(String)

 

how do i check to see if the to.Addresses field contains that string/email?

Best Answer chosen by Admin (Salesforce Developers) 
spraetzspraetz
Set<String> addresses = new Set<String>();
addresses.addAll(email.toAddresses);
boolean contains = addresses.contains('your@address.here');

 

Switch it to a set and use the .contains() method.  Or you could iterate through each value of the List like so:

 

boolean found = false;
for(String s : email.addresses){
    if(s == 'your@address.here'){
        found = true;
        break;
    }
}
return found;

 

All Answers

spraetzspraetz
Set<String> addresses = new Set<String>();
addresses.addAll(email.toAddresses);
boolean contains = addresses.contains('your@address.here');

 

Switch it to a set and use the .contains() method.  Or you could iterate through each value of the List like so:

 

boolean found = false;
for(String s : email.addresses){
    if(s == 'your@address.here'){
        found = true;
        break;
    }
}
return found;

 

This was selected as the best answer
Glen.ax1034Glen.ax1034

that would make sense. then i raise you a question. i started getting an error that caused me to check if the toaddress is null.

 

        	Set<String> addresses = new Set<String>(); 
        	
        	if (email.toAddresses != null)
        		addresses.addAll(email.toAddresses); 

 

so.. now my question is... how do i write a test class so that it isn't null?I'm getting 100% test coverage except for the line above: addresses.addAll(email.toAddresses); because it's pulling a null on the email.toAddresses. That is the perceived problem.

 

	   email.subject = 'Alert - Order Complete-Attention Required';
	   env.fromAddress = 'user@acme.com';
	   env.toAddress = 'abc@123.in.salesforce.com';

 

spraetzspraetz

You could do something like 

 

if(System.isRunningTests() && email.toAddresses == null){

    email.toAddresses = new List<String>{'foo@foo.com'};

}

 

before your check, then when you're running tests and email.toAddresses == null, you will populate the toAddresses with an email address to use later.