+ Start a Discussion
Abhishek Kumar 149Abhishek Kumar 149 

how to write test class for the below code

  //Set Subject
        subject = (action == '1' || action == '2') ? 'RE: ' : ((action == '3' ) ? 'FW: ' : '');
        System.debug(subject);
        emailBody = '\n\n' + 'Thanks & Regards' + '\n' + currentUserName + '\n';
        
        //IF record Id Present
        if(String.isNotBlank(recordId)){
            List<EmailMessage> emList = [SELECT Id, ToAddress, CcAddress, BccAddress, FromAddress,
                                         FromName, ParentId, ReplyToEmailMessageId, TextBody, Subject,
                                         EBS_FromAddress__c, EBS_Status__c
                                         FROM EmailMessage WHERE Id = :recordId LIMIT 1]; 
            if(!emList.isEmpty()){
                emailMessage = emList.get(0);
                
                if(String.isNotBlank(emailMessage.Subject))
                    subject += emailMessage.Subject;
                                
                if(String.isNotBlank(emailMessage.TextBody))
                    emailBody += emailMessage.TextBody; 
                //For REPLY ALL
                toEmail = '';
                ccEmail = '';
                bccEmail = ''; 
                //For Reply
                if(action == '1' && String.isNotBlank(emailMessage.EBS_FromAddress__c)){
                   toEmail += emailMessage.EBS_FromAddress__c;                    
                }
                //Reply All
                else if(action == '2'){
                    /*
                    if(String.isNotBlank(emailMessage.ToAddress))
                        toEmail += emailMessage.ToAddress;
                        
                    if(String.isNotBlank(emailMessage.EBS_FromAddress__c) 
                            && !toEmail.containsIgnoreCase(emailMessage.EBS_FromAddress__c)){
                        toEmail = emailMessage.EBS_FromAddress__c + ',' + toEmail;    
                    }*/
          toEmail += emailMessage.EBS_FromAddress__c;                     
                    
                    if(String.isNotBlank(emailMessage.CcAddress))
                        ccEmail += emailMessage.CcAddress;
                    
                    if(String.isNotBlank(emailMessage.BccAddress))
                        bccEmail += emailMessage.BccAddress;
                    
                }
                toEmail = toEmail.replace(';', ',');                
                ccEmail = ccEmail.replace(';', ',');
                bccEmail = bccEmail.replace(';', ',');
            }
            //Find attchements when Forwarding
            if(action == '3'){
                List<Attachment> attachedFiles = [SELECT Id, Name, Body, ContentType                                                   
                                                  FROM Attachment 
                                                  WHERE ParentId =: recordId];                
                if(!attachedFiles.isEmpty()){
                    isFW = true;
                    for(Attachment a: attachedFiles){
                        attachmentList.add(new AttachmentInfo(a));    
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Abhishek Kumar, I hope it will be helpful.

Please mark it as best answer if the information is informative.

Best Regards
Rahul Kumar