I have created class for sending a email and calling this "class" and "method" in new custom button..This logic works for "Detailed page Button"and for single record but how to make it work for List views for mass Email ?

This is the Class

global class outbound_Emails{
        webservice static void SendEmailNotification (){
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setToAddresses(new string[] {'[email protected]});//
    mail.setSenderDisplayName('From Name');
    mail.setSubject('Sending a Email From SFDC');
    mail.setHtmlBody('This is the Body for testing the send email function via custom button, which i should include in list view');
       Messaging.sendEmail(new messaging.SingleEmailMessage[] {mail});

This is the button


var retStr; 
retStr = sforce.apex.execute("outbound_Emails", "SendEmailNotification",{}); 

  • Out of curiosity - any special reason why you haven't used the standard "mass email" available as link on Contact & Lead tab? It has some limitations like max 250 records in 1 "batch" but still - why you wrote code for it?
    – eyescream
    Commented Jan 20, 2014 at 17:22
  • @eyescream: Nothing special...I just wanted to learn code functionality..so that I can implement the same logic for different scenario.
    – user40138
    Commented Jan 20, 2014 at 17:28

1 Answer 1


Assuming you have this button on a related list/list view, you can use the {!GETRECORDIDS} javascript function to get the list/array of id of the selected records. You can then send this array to your apex class and then query the email field to get the email. YOu can do something like this:

var recordIdArray = {!GETRECORDIDS($ObjectType.Contact)};
console.log('the record Ids are' + recordIdArray);
var retStr = sforce.apex.execute("outbound_Emails", "SendEmailNotification",{conId: recordIdArray});

global class outbound_Emails{
        webservice static void SendEmailNotification (list<id> conId){
            list<contact> conList = [SELECT name,email FROM contact WHERE id IN : conId];
            system.debug('the selected contacts are  ' + conList);
            //you can iterate over this list to get the email and send the mail
  • You only need to query the contacts if you need their name. If you're using a template, setTargetObjectIds will suffice.
    – sfdcfox
    Commented Jan 20, 2014 at 16:29
  • @sfdcfox: As the OP was using the setToAddress() method I used the query to get the email. But, Yes there is no need to query if setTargetObjectIds() is used.
    – Sam
    Commented Jan 20, 2014 at 16:37
  • @theGreatDanton thank you for your code and one more thing how to Refresh the view so that the user get to know that mails has been sent ? whether should i put code in "Button" OR "Class" ?
    – user40138
    Commented Jan 20, 2014 at 16:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .