You need to sign in to do that
Don't have an account?
Asakti
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;
}
}
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;
}
}
Please try the below test class:
Thanks,
Maharajan.C
All Answers
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.
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
Please try the below test class:
Thanks,
Maharajan.C
Please Mark it as Best Answer if it helps.
Thanks