• Louis-Paul Valadoux
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies
Hello,

I am trying to send an email to all selected users in a related view. My issue is that I don't understand why my email is not sending when I call sendEmail from MyAccountIDExt while it's working well when I call it directly from EmailUtil. Any ideas why ? 

Here are my codes - please note that I've changed the email adresses:
MyAccountIDExt
public class MyAccountIDExt
{
    public List<Pipe_Owner__c> memList {get;set;}
    private ApexPages.StandardSetController standardController;
    private Set<Id> memIds = new Set<Id>();

    public MyAccountIDExt(ApexPages.StandardSetController standardController){
        this.standardController = standardController;
        
        memList = new List<Pipe_Owner__c>();
        for (Pipe_Owner__c mem : (List<Pipe_Owner__c>)standardController.getSelected()){
            memIds.add(mem.Id);
        	
        }
        memList = [SELECT Name, eMail_address__c FROM Pipe_Owner__c WHERE ID IN: memIds];
        
        throweMailMethod('LPV', 'test@company.com'); 
   
    }
        
    public void throweMailMethod(String valName, String valeMail){
		  
		List<String> toAddresses = new List<String> {'test@company.com'};
        //toAddresses.add(Pipe_Owner__c.eMail_address__c);
        
        String replyToAddress = 'test@company.com';
        
        //use the new util class to send an email
        EmailUtil emailUtil = new EmailUtil(toAddresses);
        
        //send plain text body
        emailUtil.plainTextBody(valName + ', - This is a test.')
             .senderDisplayName('Administrator')
             .replyTo(replyToAddress)
             .sendEmail();

    }

}

EmailUtil:
public class EmailUtil {
       private Messaging.SingleEmailMessage singleEmailMessage;
       private final List<String> toAddresses;
       
       //optional parameters set to default        
       private String subject = '';
       private String htmlBody = ''; 
       private Boolean useSignature = false;
       private List<Messaging.EmailFileAttachment> fileAttachments = null;
       //defaults to current user's first name + last name
       private String senderDisplayName = UserInfo.getFirstName()+' '+UserInfo.getLastName();
       //get the current user in context
       User currentUser = [Select email from User where username = :UserInfo.getUserName() limit 1];        
       //replyTo defaults to current user's email 
       private String replyTo = currentUser.email;
       private String plainTextBody = '';
       
       public EmailUtil(List<String> addresses) {
           this.toAddresses = addresses;
       }
       
       public EmailUtil senderDisplayName(String val) {
           senderDisplayName = val;
           return this;
       }
       
       public EmailUtil subject(String val) {
           subject = val;
           return this;
       }
       
       public EmailUtil htmlBody(String val) {
           htmlBody = val;
           return this;
       }
       
       public EmailUtil useSignature(Boolean bool) {
           useSignature = bool;
           return this;
       }
       
       public EmailUtil replyTo(String val) {
           replyTo = val;
           return this;
       }
       
       public EmailUtil plainTextBody(String val) {
           plainTextBody = val;
           return this;
       }
       
       public EmailUtil fileAttachments(List<Messaging.Emailfileattachment> val) {
           fileAttachments = val;
           return this;
       }
       
       //where it all comes together
       //this method is private and is called from sendEmail()
       private EmailUtil build() {
           singleEmailMessage = new Messaging.SingleEmailMessage();
           singleEmailMessage.setToAddresses(this.toAddresses);
           singleEmailMessage.setSenderDisplayName(this.senderDisplayName);
           singleEmailMessage.setSubject(this.subject);
           singleEmailMessage.setHtmlBody(this.htmlBody);
           singleEmailMessage.setUseSignature(this.useSignature);
           singleEmailMessage.setReplyTo(this.replyTo);
           singleEmailMessage.setPlainTextBody(this.plainTextBody);
           singleEmailMessage.setFileAttachments(this.fileAttachments);
           return this;
       }
       
       //send the email message
public void sendEmail() {

        // build and send email.

        build();

        last_sendEmail_result = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { singleEmailMessage });

    }
public static Messaging.SendEmailResult[] last_sendEmail_result {get; private set;}

//custom exception		
public class GenericException extends Exception{
	
}    
           
   }

and the VF page:
<!--<apex:page controller="TestSendEmail" action="{!throweMailMethod}">
<apex:form>
<apex:inputHidden value="{!case.OwnerId}"/>
</apex:form> 
</apex:page>-->

<apex:page standardController="Pipe_Owner__c" extensions="MyAccountIDExt" recordSetVar="Member">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable value="{!memList}" var="M">
            <apex:column value="{!M.Name}"/>
            <apex:column value="{!M.eMail_address__c}"/>

        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>
</apex:page>

 
Hello,
I have two custom tables (opportunity__c and Pipe_Owner__c).
When an opportunity is created, it should be linked to only one Pipe Owner. 
I would like to create a custom button on the Pipe Owner table which send a separate custom email to all of the selected owners. In this email I would like to have the list of all related opportunities that are linked to this owner.

e.g : 
Dear XXX, 
Please find below your open pipe:
Opp 1 | Account Name | Amount
Opp 2 | Account Name | Amount
etc..

I'm new to the Salesforce environment, but now I have done the following:
- Create a custom button which is launching a VisualForce page and sending an email to myself (but with hard coded values)

I would like now to be able to send one separate email to each of selected records with inside the body the opportunities related to this record. How can I do that ? Do you have some sample code or documentation ?

I'm using lightning version. 

Many thanks,