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
Nisha110687Nisha110687 

Sending Emails based on related lists

Hi,

The requirement is as below:

There are 2 Objects : A and B

Various page layouts are there in A and a related list of B is thr in A.

 

On A's Page layouts we can create mutliple instances of B ( B has a few text fields including email).

 

 Depending on the user, he can had 1 or more B's objects to A's layout and enter the details. The Problem : We need to send out emails to each of B's records added.

 

For Ex: while creating a records in A, the user can add say 3 B records.(B1,B2,B3) Whn the save button is clicked, we need to send out emails to B1,B2 and B3.

 

How can this be done?

Best Answer chosen by Admin (Salesforce Developers) 
RavanRavan

Awesome requirement...

 

From what I see here, I am putting a pseudo code for you. I belive this is possible.

 

1) Write a before insert trigger on object A.

2) Call a external webservice API for mass email.

Note:. You can write this in Java or APex

3) Pass an array of object b email ids.

 

java code:

 

MassEmailMessage[] messages = new MassEmailMessage[1];
    messages[0] = new MassEmailMessage(); 
    messages[0].setBccSender(bccSender);
    messages[0].setEmailPriority(EmailPriority.Normal);
    messages[0].setReplyTo(replyTo);
    messages[0].setSaveAsActivity(saveAsActivity);
    messages[0].setSubject(emailSubject);
    messages[0].setUseSignature(useSignature);
    messages[0].setTemplateId(new ID(templateId));
  
   
    ID[] contactIds = new ID[1];
   
    contactIds[0] = new ID(contactId);
   
    //verify contact id is not null prior to attempting email
    if(contactIds[0]!=null) {
     messages[0].setTargetObjectIds(contactIds);

     try {
        SendEmailResult[] result = sfdc.sendEmail(messages);
    }

 

Hope it helps.

http://www.siddheshkabe.co.in