[SalesForce] How to send Mass Email from List View in Contact Or Lead using custom button

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[] {'abc@gmail.com});//
    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

{!requireScript("/soap/ajax/20.0/connection.js")} 
{!requireScript("/soap/ajax/20.0/apex.js")} 

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


 alert(retStr);

Best Answer

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:

{!REQUIRESCRIPT("/soap/ajax/28.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/28.0/apex.js")}
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
        }
}
Related Topic