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
Rocio VivancoRocio Vivanco 

How to use custom settings in APEX code for sending emails?

Hi all,

I have been trying to find out how can I use custom settings in apex code when it comes to use the sendEmail function.
For example, I have the following code lines:
 
global void sendEmail(){

 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 // Assign the addresses for the To and CC lists to the mail object.
        mail.setToAddresses('myToAddress@email.com');
        
        // Specify the address used when the recipients reply to the email. 
        mail.setReplyTo('myToAddress@email.com');
        
        // Specify the name used as the display name.
        mail.setSenderDisplayName('Salesforce Email Reminder');
        
        // Specify the subject line for your email address.
        mail.setSubject('Yearly Email Reminder');
        
        // Set to True if you want to BCC yourself on the email.
        mail.setBccSender(false);
        
        mail.setUseSignature(false);
    
        
        mail.setHtmlBody('This is a test email');
I have created a Custom Setting Test__c with the following fields and values in one record:

- ToAddress = 'myToAddress@email.com'
- replyToAddress = 'myToAddress@email.com'
- SenderDisplayName = 'Salesforce Email Reminder'
- Subject = 'Yearly Email Reminder'
- Body = 'This is a test'

Now, how can I replace the values in the parethesis for the respective email methods mail.setToAddresses(), etc. by the above mentioned custom setting. How can I invoke these custom setting values in the APEX code?. 

I would appreciate your answers.

 
Best Answer chosen by Rocio Vivanco
Manj_SFDCManj_SFDC
Please make address field as a string array
String toEmailAddress= cs.ToEmailAddress__c;
String[] sendTo = new String[]{toEmailAddress};
mail.setToAddresses(sendTo);

All Answers

Manj_SFDCManj_SFDC
Hey Rocio,

yes you can replace the hard coded values with the dyanmic values from custom setting , you can do this

Custom_Settings__c cs = Custom_Settings__c.getInstance('ToAddress');
String toAddr = cs.Email__C; // use your field's API name

and instead of this mail.setToAddresses('myToAddress@email.com'); you can use 
mail.setToAddresses(toAddr);

Please mark the answer, if it helps you 
good luck 

 
v varaprasadv varaprasad
Hey Rocio,

Please check once following sample code : 
 
Test__c myCS1 = Test__c.getValues('EmailData');
String toAddress = myCS1.ToAddress__c;
String replyToAdd = myCS1.replyToAddress__c;
String senderDisplayName = myCS1.SenderDisplayName__c;
String subject = myCS1.Subject__c;
String body = myCS1.Body__c;


system.debug('==toAddress=='+toAddress);
system.debug('==replyToAdd=='+replyToAdd);
system.debug('==senderDisplayName=='+senderDisplayName);
system.debug('==subject=='+subject);
system.debug('==body=='+body);


Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 
Rocio VivancoRocio Vivanco
Thanks guys for your answers!!! :). Only two questions:

@varaprasad: On the part Test__c.getValues('EmailData'); what would be 'EmailData' in my case?.

For example, I created the custom setting with the fields. Then I created a record, when you create the record you have to give it a name, in my case the name is "List". Is that what you mean with 'EmailData'?

@the same manj_sfdc: Custom_Settings__c.getInstance('ToAddress');

Here you mean:

Custom_Settings__c.getInstance('List');

in my case?
Rocio VivancoRocio Vivanco
I got it. I am getting the error:

Method does not exist or incorrect signature: void setToAddresses(String) from the type Messaging.SingleEmailMessage

on the line:
 
mail.setToAddresses('myToAddress@email.com');

when I try to replace it through the variable:
 
String toEmailAddress= cs.ToEmailAddress__c;

so, this way:
 
mail.setToAddresses(toEmailAddress);

Can please somebody help me? what am I doing wrong here?​
 
Manj_SFDCManj_SFDC
Please make address field as a string array
Manj_SFDCManj_SFDC
Please make address field as a string array
String toEmailAddress= cs.ToEmailAddress__c;
String[] sendTo = new String[]{toEmailAddress};
mail.setToAddresses(sendTo);
This was selected as the best answer
Rocio VivancoRocio Vivanco
Thanks for responding. Now I am getting the same error for:

mail.setReplyTo(replyToAddress);

I declared the variable this way:

String [] replyToAddress = new String [] {cs.replytoAddress__c};

What am I doing wrong?
Manj_SFDCManj_SFDC
you dont have to use the array for replyToAddress pass it as just a  String
v varaprasadv varaprasad
You need to pass variable only like below   :

 
mail.setToAddresses(toAddress);

 
Rocio VivancoRocio Vivanco
Thanks varaprasad, before your answer I adjusted it and it worked.