• Anjum Attar
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
//@author : Anjum Attar
//Creation Date :
//Modification History :
public with sharing class EmailOppInvoice {
    public Opportunity oppRecord{get;set;}
    public String id{get;set;}
    public String eSubject{get;set;}
    public String eBody{get;set;}
    //public  Contact objcon = new Contact();
    
    public EmailOppInvoice(ApexPages.StandardController controller) {
        oppRecord = (Opportunity)controller.getRecord();
        id = oppRecord.id;
        oppRecord = [Select Id,Name,Email__c,Account.Id From Opportunity Where Id =: id];
        eSubject ='';
        eBody = '';
    }
    
    public EmailOppInvoice() {
    }
    
    public pageReference SendEmail() {
       Messaging.singleEmailMessage email = new Messaging.singleEmailMessage();
       PageReference pdfExample = new pagereference('/apex/OpportunityInvoice?id='+id);  
       pdfExample.setRedirect(true);
        
        Blob content = pdfExample.getContent();
        Messaging.emailFileAttachment fileAttachment = new Messaging.emailFileAttachment();
        fileAttachment.setFileName('Invoice.pdf');
        fileAttachment.setBody(content);
        String[] eAddress = new List<String>();
        try {
            eAddress.add(oppRecord.Email__c);
            email.setSubject(eSubject);
            email.setPlainTextBody(eBody);
            email.setToAddresses(eAddress);
            email.setFileAttachments(new Messaging.EmailFileAttachment[] {fileAttachment});
            Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
            return null;
        }
        catch(Exception e) {
             ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Email Field of Account does not contain any value'));
        }
        return null;
    }
}
//@author : Anjum Attar
//Creation Date :
//Modification History :
public with sharing class EmailOppInvoice {
    public Opportunity oppRecord{get;set;}
    public String id{get;set;}
    public String eSubject{get;set;}
    public String eBody{get;set;}
    //public  Contact objcon = new Contact();
    
    public EmailOppInvoice(ApexPages.StandardController controller) {
        oppRecord = (Opportunity)controller.getRecord();
        id = oppRecord.id;
        oppRecord = [Select Id,Name,Email__c,Account.Id From Opportunity Where Id =: id];
        eSubject ='';
        eBody = '';
    }
    
    public EmailOppInvoice() {
    }
    
    public pageReference SendEmail() {
       Messaging.singleEmailMessage email = new Messaging.singleEmailMessage();
       PageReference pdfExample = new pagereference('/apex/OpportunityInvoice?id='+id);  
       pdfExample.setRedirect(true);
        
        Blob content = pdfExample.getContent();
        Messaging.emailFileAttachment fileAttachment = new Messaging.emailFileAttachment();
        fileAttachment.setFileName('Invoice.pdf');
        fileAttachment.setBody(content);
        String[] eAddress = new List<String>();
        try {
            eAddress.add(oppRecord.Email__c);
            email.setSubject(eSubject);
            email.setPlainTextBody(eBody);
            email.setToAddresses(eAddress);
            email.setFileAttachments(new Messaging.EmailFileAttachment[] {fileAttachment});
            Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
            return null;
        }
        catch(Exception e) {
             ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Email Field of Account does not contain any value'));
        }
        return null;
    }
}