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
AsaktiAsakti 

how to write a test class for field update depending on the email received

below is my class for updating the field as per the email reply ,can someone help me with the test class for the same 


global class UpdateStatusOnCertificate implements Messaging.InboundEmailHandler {
    
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, 
                                                           Messaging.InboundEnvelope env){
                                                               
                                                               // Create an InboundEmailResult object for returning the result of the 
                                                               // Apex Email Service
                                                               Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
                                                               
                                                               
                                                               
                                                               Certificate__c vCon = [SELECT Id, Status__c, Personal_Email_id__c,LastModifiedDate 
                                                                                      FROM Certificate__c
                                                                                      WHERE Personal_Email_id__c = :email.fromAddress
                                                                                      order by LastModifiedDate desc LIMIT 1];
                                                               
                                                               string  RepliedMsgOnly = email.plainTextBody.substring(0, 20);    
                                                               
                                                               if(RepliedMsgOnly.contains('Acknowledged'))
                                                               {
                                                                   
                                                                   vCon.Status__c = 'Acknowledged';
                                                                   vCon.Acknowledged__c = true;
                                                               }
                                                               else if(RepliedMsgOnly.contains('Not Received'))
                                                               {
                                                                   vCon.Status__c = 'Not Received';
                                                                   vCon.Acknowledged__c = false;     
                                                               }
                                                               update vCon;
                                                               
                                                               // Set the result to true. No need to send an email back to the user 
                                                               // with an error message
                                                               result.success = true;
                                                               
                                                               // Return the result for the Apex Email Service
                                                               return result;
                                                           }
}




test class:
@isTest
public class UpdateStatus_test {
   public static testMethod void updatecheck()
   {
           Messaging.InboundEmail email = new Messaging.InboundEmail();
        email.subject = 'testing';
        email.plainTextBody = 'Hello, this a test email body. for testing purposes only. Bye';
       
           Certificate__c vCon = [SELECT Id, Status__c, Personal_Email_id__c,LastModifiedDate 
        FROM Certificate__c limit 1];
               vCon.Status__c = 'Acknowledged';
            vCon.Acknowledged__c = true;
       update vCon;

   }
}
Best Answer chosen by Asakti
Maharajan CMaharajan C
Hi Asakti,

Please try the below test class:
 
@isTest
public class UpdateStatus_test {
    public static testMethod void TestEmaildata1(){
		Certificate__c cert = new Certificate__c();
        cert.Status__c = 'Requested Certificate Sent By Admin';
        cert.Personal_Email_id__c = 'asakti.dhir@absyz.com';
        insert cert; 
		
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        email.subject = 'Test Email';
        email.fromAddress = 'asakti.dhir@absyz.com';
        email.fromName = 'Test Candidate';
        email.plainTextBody = 'Acknowledged email body\n2225256325\nTitle';
        
        UpdateStatusOnCertificate callingclass = new UpdateStatusOnCertificate();
		callingclass.handleInboundEmail( email, env);
    }
    
    public static testMethod void TestEmaildata2(){
        Certificate__c cert = new Certificate__c();
        cert.Status__c = 'Requested Certificate Sent By Admin';
        cert.Personal_Email_id__c = 'asakti.dhir@absyz.com';
        insert cert; 
		
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        email.subject = 'Test Email';
        email.fromAddress = 'asakti.dhir@absyz.com';
        email.fromName = 'Test Candidate';
        email.plainTextBody = 'Not Received email body\n2225256325\nTitle';
        
        UpdateStatusOnCertificate callingclass = new UpdateStatusOnCertificate();
		callingclass.handleInboundEmail( email, env);
    }
}

Thanks,
Maharajan.C

All Answers

ShivankurShivankur (Salesforce Developers) 
Hi Asakti,

You will need to add/create test data into test class for all your use cases.

Likewise, you have different conditions in your apex logic to check if the replied email message contains values like Acknowledged, Not Received, etc. There should be multiple records which should be passed to the test to cover the Apex logic fully.

To learn more on this:
https://trailhead.salesforce.com/en/content/learn/modules/unit-testing-on-the-lightning-platform/generate-data-for-tests

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.
AsaktiAsakti
@isTest
public class UpdateStatus_test {
    public static testMethod void CreateCertificateData(){
        Certificate__c acc = new Certificate__c();
        acc.Status__c = 'Requested Certificate Sent By Admin';
        acc.Personal_Email_id__c = 'asakti.dhir@absyz.com';
        insert acc; 
        
    }
        static void updateCertificate() {
        Test.startTest();
        Certificate__c acc = [SELECT    Id 
                               FROM     Certificate__c 
                               LIMIT     1];
        acc.Status__c = 'Acknowledged';
        update acc;
        Test.stopTest();
    }
    public static testMethod void CreateEmaildata(){
        // Create a new email
        
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        
        email.subject = 'Create Draft';
        email.fromAddress = 'someaddress@email.com';
        email.plainTextBody = 'email body\n2225256325\nTitle';
        string  RepliedMsgOnly = 'Recieved';  
        UpdateStatusOnCertificate callingclass = new UpdateStatusOnCertificate();
        callingclass.handleInboundEmail( email, env);
    }
    
    
}


it is showing only 28%,can you please help me on this
 
Maharajan CMaharajan C
Hi Asakti,

Please try the below test class:
 
@isTest
public class UpdateStatus_test {
    public static testMethod void TestEmaildata1(){
		Certificate__c cert = new Certificate__c();
        cert.Status__c = 'Requested Certificate Sent By Admin';
        cert.Personal_Email_id__c = 'asakti.dhir@absyz.com';
        insert cert; 
		
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        email.subject = 'Test Email';
        email.fromAddress = 'asakti.dhir@absyz.com';
        email.fromName = 'Test Candidate';
        email.plainTextBody = 'Acknowledged email body\n2225256325\nTitle';
        
        UpdateStatusOnCertificate callingclass = new UpdateStatusOnCertificate();
		callingclass.handleInboundEmail( email, env);
    }
    
    public static testMethod void TestEmaildata2(){
        Certificate__c cert = new Certificate__c();
        cert.Status__c = 'Requested Certificate Sent By Admin';
        cert.Personal_Email_id__c = 'asakti.dhir@absyz.com';
        insert cert; 
		
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        email.subject = 'Test Email';
        email.fromAddress = 'asakti.dhir@absyz.com';
        email.fromName = 'Test Candidate';
        email.plainTextBody = 'Not Received email body\n2225256325\nTitle';
        
        UpdateStatusOnCertificate callingclass = new UpdateStatusOnCertificate();
		callingclass.handleInboundEmail( email, env);
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
AsaktiAsakti
thank you maharajan,it worked .
Suraj Tripathi 47Suraj Tripathi 47
Hi Askti dhir,
 
global class UpdateStatusOnCertificate implements Messaging.InboundEmailHandler {
    
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){
                                                               
                                                               
    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
                                                               
                                                               
                                                               
   Certificate__c vCon = [SELECT Id,Status__c,Personal_Email_id__c,LastModifiedDate 
                                        FROM Certificate__c WHERE 
                                       Personal_Email_id__c=:env.fromAddress order by 
                                        LastModifiedDate desc LIMIT 1];
                                                               
    string  RepliedMsgOnly = email.plainTextBody.substring(0, 20);    
                                                               
    if(RepliedMsgOnly.contains('Acknowledged'))
     {
          vCon.Status__c = 'Acknowledged';
          vCon.Acknowledged__c = true;
      }
     else if(RepliedMsgOnly.contains('Not Received'))
      {
          vCon.Status__c = 'Not Received';
          vCon.Acknowledged__c = false;     
       }

      update vCon;
      result.success = true;
                                                               
      return result;
    }
}




//test class

@isTest
public class UpdateStatus_test {
    static testMethod void updatecheck()
    {
        Messaging.InboundEmail email = new Messaging.InboundEmail();
        Messaging.InboundEnvelope env    = new Messaging.InboundEnvelope();
        email.plainTextBody = 'Hello Acknowledged, this a test email body. for testing purposes only. Bye';
        
        Certificate__c vCon = new Certificate__c(name='ggg',
                                                 Status__c='Acknowledged',
                                                 Acknowledged__c = true,
                                                 Personal_Email_id__c='user@acme.com');
        insert vCon;
        email.subject = 'testing';
        env.fromAddress = 'user@acme.com';  
        UpdateStatusOnCertificate Obj = new UpdateStatusOnCertificate();
        Obj.handleInboundEmail(email, env ); 
    }
    static testMethod void updatecheck1()
    {
        Messaging.InboundEmail email = new Messaging.InboundEmail();
        Messaging.InboundEnvelope env    = new Messaging.InboundEnvelope();
        email.plainTextBody = 'Not Received, this a test email body. for testing purposes only. Bye';
        
        Certificate__c vCon = new Certificate__c(name='ggg',
                                                 Status__c='Acknowledged',
                                                 Acknowledged__c = true,
                                                 Personal_Email_id__c='user@acme.com');
        insert vCon;
        email.subject = 'testing';
        env.fromAddress = 'user@acme.com';  
        UpdateStatusOnCertificate Obj = new UpdateStatusOnCertificate();
        Obj.handleInboundEmail(email, env ); 
    }
}

Please Mark it as Best Answer if it helps.
Thanks
AsaktiAsakti
Thank you for your response.