2

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);
  • 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 Jan 20 '14 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 Jan 20 '14 at 17:28
4

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
        }
}
  • You only need to query the contacts if you need their name. If you're using a template, setTargetObjectIds will suffice. – sfdcfox Jan 20 '14 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 Jan 20 '14 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 Jan 20 '14 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.