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
Neeraj Sharma 103Neeraj Sharma 103 

How to Write test Class for below Apex Class Controller Please Replay ASAP

public class EmailInvoiceA {
    
    @auraEnabled
    public static void sendMailWithPDF(String recordId){
        for(Invoice__c cc : [select Id,Distributor__r.E_mail__c,Distributor__r.CC_to_if_any__c,Distributor__r.Name from Invoice__c WHERE Id =: recordId]){
            list<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();  
            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
            List<String> sendTo = new List<String>();
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            PageReference pref = page.PdfEmailInvoice;
            pref.getParameters().put('Id',recordId);
            pref.setRedirect(true);
            Blob b;
            b = pref.getContent();
            attach.setFileName('Invoice.pdf');
            attach.setBody(b);
            semail.setSubject('Invoice details');
            semail.setReplyTo('noreply@gmail.com');
            semail.setSenderDisplayName('salesforce User');
           sendTo.add(cc.Distributor__r.E_mail__c);
            
            List<String> send = new List<String>();
            if(cc.Distributor__r.E_mail__c==null && cc.Distributor__r.CC_to_if_any__c != null)
            {  
                send.add(cc.Distributor__r.CC_to_if_any__c);
               semail.setToAddresses(send);
            }
            else if(cc.Distributor__r.E_mail__c !=null && cc.Distributor__r.CC_to_if_any__c != null)
            {
                sendTo.add(cc.Distributor__r.E_mail__c);
                send.add(cc.Distributor__r.CC_to_if_any__c);
              semail.setccAddresses(send);
              semail.setToAddresses(sendTo);
                
           }
           else if(cc.Distributor__r.E_mail__c !=null && cc.Distributor__r.CC_to_if_any__c == null)
            {  
              sendTo.add(cc.Distributor__r.E_mail__c);
                   semail.setToAddresses(sendTo);
         }
                mails.add(semail);
            String emailBody =' <html><body><p> Dear '+ cc.Distributor__r.Name+' ,<br><br> Please find the attached Invoice details. <br><br>    Regards, <br> IT Team <br>  Sayaji Groups.</p></body></html>';
            semail.setHtmlBody(emailBody);
            semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
        }
    }      
    @auraEnabled
     public static void sendMailWith(String recordId,String mMail){
        for(Invoice__c cc : [select Id,Distributor__r.E_mail__c,Distributor__r.CC_to_if_any__c,Distributor__r.Name from Invoice__c WHERE Id =: recordId]){
            list<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();  
            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
            List<String> sendTo = new List<String>();
            sendTo.add(mMail);
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            PageReference pref = page.PdfEmailInvoice;
            pref.getParameters().put('Id',recordId);
            pref.setRedirect(true);
            Blob b;
            b = pref.getContent();
            attach.setFileName('Invoice.pdf');
            attach.setBody(b);
            semail.setSubject('Invoice details');
            semail.setReplyTo('noreply@gmail.com');
            semail.setSenderDisplayName('salesforce User');
           
           
              List<String> send = new List<String>();
            if(cc.Distributor__r.CC_to_if_any__c != null)
            {
                 send.add(cc.Distributor__r.CC_to_if_any__c);
                 
            }
            mails.add(semail);
			semail.setCcAddresses(send);
            semail.setToAddresses(sendTo);
            String emailBody =' <html><body><p> Dear '+ cc.Distributor__r.Name+' ,<br><br> Please find the attached Invoice details. <br><br>    Regards, <br> IT Team <br>  Sayaji Groups.</p></body></html>';
            semail.setHtmlBody(emailBody);
            semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
        }
    }    
     @auraEnabled
   public static void Mail(String recordId,String mMail,boolean checkCmp){
        for(Invoice__c cc : [select Id,Distributor__r.E_mail__c,Distributor__r.CC_to_if_any__c,Distributor__r.Name from Invoice__c WHERE Id =: recordId]){
            list<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();  
            Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
            List<String> sendTo = new List<String>();        
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            PageReference pref = page.PdfEmailInvoice;
            pref.getParameters().put('Id',recordId);
            pref.setRedirect(true);
            Blob b;
            b = pref.getContent();
            attach.setFileName('Invoice.pdf');
            attach.setBody(b);
            semail.setSubject('Invoice details');
            semail.setReplyTo('noreply@gmail.com');
            semail.setSenderDisplayName('salesforce User');
  
           if(checkCmp==true)
               {
                  sendTo.add(mMail);
                   semail.setToAddresses(sendTo);
               }
           else  if(checkCmp==false)
            {
              if(mMail != null)
            {
                sendTo.add(mMail);
            	semail.setToAddresses(sendTo);
            }   
             if(cc.Distributor__r.E_mail__c != null)
            {
               sendTo.add(cc.Distributor__r.E_mail__c);  
                semail.setToAddresses(sendTo);
            } 
                
             if(cc.Distributor__r.CC_to_if_any__c != null)
            { 
                List<String> send = new List<String>();//cc string
				send.add(cc.Distributor__r.CC_to_if_any__c);  
               semail.setCcAddresses(send);
           }   
            }
            mails.add(semail);

            String emailBody =' <html><body><p> Dear '+ cc.Distributor__r.Name+' ,<br><br> Please find the attached Invoice details. <br><br>    Regards, <br> IT Team <br>  Sayaji Groups.</p></body></html>';
            semail.setHtmlBody(emailBody);
            semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
        }
    }       
}

 
Best Answer chosen by Neeraj Sharma 103
Ajay K DubediAjay K Dubedi
Hi Neeraj,
Have a look at this thread the same question with the solution is here:
https://developer.salesforce.com/forums?id=9062I000000IIBVQA4
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi

All Answers

Avishek Nanda 14Avishek Nanda 14
Hi Neeraj,

The create a test record and insert it in Test Class. and Call the method of the class and pass parameter of this record id.. Then you should be good to go. 

Regards,
Avishek
Ajay K DubediAjay K Dubedi
Hi Neeraj,
Have a look at this thread the same question with the solution is here:
https://developer.salesforce.com/forums?id=9062I000000IIBVQA4
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
This was selected as the best answer