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
Philip GulanPhilip Gulan 

What is the best solution to create a Button to send email to multiple contacts in Contact Related List?

I need to create a button on contact related list so that I can send email to selected multiple contacts. What would be the best solution and best practices?
Best Answer chosen by Philip Gulan
Mark Khabosha 9Mark Khabosha 9
Hi,

This is also the Apex Class 
 
global class myEmailClass
{
    webservice static void emailContact(list<id> conId) // you can pass parameters
    { 
   //Parameters getting from the URL
     List<Contact> contactNames = [SELECT name, email FROM contact WHERE id IN : conId];
   //system.debug('the selected contacts are  ' + contactNames);
        
   //Methods to send Mass Email
     Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
     mail.setTargetObjectIds(conId);
     mail.setTemplateId('00X41000000UYWl'); //ID for Email Template
     Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
     }  
}

I have tested this and works perfectly fine. so resut assure about the results.

Regards,
Mark Khabosha

All Answers

Lucas Duque 9Lucas Duque 9
Hello Philip !

Well, to make it completely I don't know how, but I know that you'll need  to create a Visualforce Page with a custom controller (that would be a class Apex with method to send email) and a Custom Button in the Object (Contact) which is the easiest. 

However, you can do the next, on related list, put a custom button in Contact Object that Update a field of Contact  and select the contacts which you want, for example, the field "Contact Status", and then when the value of this field updated for "Sent", with automation process Worflow Rules, create an action on Worflow rules that send email when the "Contact Status" is equal to "Sent". This way to solve your problem eliminates the need to create an Apex Class, and you'll need only a Custom Button in Object Contact and a Visualforce Page with Standardcontroller = "Contact".

So, I really don't know how do that completely, but I thought very interesting this question and I hope that someone can answer, cause I would like know how do that.

I hope I have been helpful in something!

Good work !
@Karanraj@Karanraj
If you want to send automated email to the selected contacts from the related list, then you can make use of java script button.
Create a java script button list button and select display checkboxes(for multi-Record selection)
{!REQUIRESCRIPT('/soap/ajax/35.0/connection.js')}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
varselectedContactIds = {!GETRECORDIDS($ObjectType.Contact)};
sforce.apex.execute("myEmailClass","emailContact",{Id:varselectedContactIds});
myEmailClass - Its the apex class
emailContact - method name
{Id:varselectedContactIds} - method parameter
//Apex Class
global class myEmailClass
{
    webservice static void emailContact() // you can pass parameters
    { 
         //Write logic to send email
    }
}

If you want user to type the content of the email after clicking the button then you have to go with visualforce page and controller for the email logic
Mark Khabosha 9Mark Khabosha 9
Hi, 

This is the corrected code for the java script button in the contract related list.
 
{!REQUIRESCRIPT('/soap/ajax/35.0/connection.js')} 
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} 

var ids = {!GETRECORDIDS($ObjectType.Contact)}; 

if (ids.length) { 
     if (ids.length <= 100) { 

           sforce.apex.execute("myEmailClass","emailContact",{conId: ids}); 
           alert("The Ids you have selected are: "+ids); 

        } else { 
            alert('Select 100 or less'); 
        } 
        } else { 
             alert('Select one or more Contacts'); 
}
Please let me know if you have any questions.

Mark

 
Mark Khabosha 9Mark Khabosha 9
Hi,

This is also the Apex Class 
 
global class myEmailClass
{
    webservice static void emailContact(list<id> conId) // you can pass parameters
    { 
   //Parameters getting from the URL
     List<Contact> contactNames = [SELECT name, email FROM contact WHERE id IN : conId];
   //system.debug('the selected contacts are  ' + contactNames);
        
   //Methods to send Mass Email
     Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
     mail.setTargetObjectIds(conId);
     mail.setTemplateId('00X41000000UYWl'); //ID for Email Template
     Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
     }  
}

I have tested this and works perfectly fine. so resut assure about the results.

Regards,
Mark Khabosha
This was selected as the best answer
Brooke HarperBrooke Harper
Thanks Mark! I hope this would work like a charm once I test this out. Really appreciate it.