You need to sign in to do that
Don't have an account?

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:
- 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.
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.
String toEmailAddress= cs.ToEmailAddress__c;
String[] sendTo = new String[]{toEmailAddress};
mail.setToAddresses(sendTo);
All Answers
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
Please check once following sample code :
Hope this helps you!
Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
@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?
Method does not exist or incorrect signature: void setToAddresses(String) from the type Messaging.SingleEmailMessage
on the line:
when I try to replace it through the variable:
so, this way:
Can please somebody help me? what am I doing wrong here?
String toEmailAddress= cs.ToEmailAddress__c;
String[] sendTo = new String[]{toEmailAddress};
mail.setToAddresses(sendTo);
mail.setReplyTo(replyToAddress);
I declared the variable this way:
String [] replyToAddress = new String [] {cs.replytoAddress__c};
What am I doing wrong?