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
Vidya H 4Vidya H 4 

need to write test class for this code plz help

trigger sendMailToContactTrigger on Contact (after insert) {
    set<String> emailSet = new Set<String>();
    EmailTemplate temp=[Select id,Body from EmailTemplate where name='contact text email template'];
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    if(Trigger.IsAfter && Trigger.IsInsert){
        for(Contact Con : Trigger.new){
            if(Con.Email != Null){
                 Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
                singleMail.setHtmlBody(temp.Body);
                singleMail.setToAddresses(new List<String>{con.Email});
                singleMail.setTargetObjectId(con.Id);
                singleMail.setTemplateId(temp.Id);
                emails.add(singleMail);
            }
        }
    }
    if(emails.Size()>0){
    Messaging.sendEmail(emails);
    }
}
Best Answer chosen by Vidya H 4
Maharajan CMaharajan C
Hi Vidya,

Please try the below test class:
 
@isTest
public class sendMailToContactTriggerTest {
    static testmethod void testsendMailToContact(){
        
        // Add if there is any other required for Account creation
        Account acc = new Account(name='Test Account');
        insert acc;
        
        // Add if there is any other required for Contact creation
        Contact con = new Contact(LastName = 'Test Contact', Email = 'TestCon@gmail.com');
        
        Test.startTest();
        	insert con;
        Test.stopTest();
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Vidya,

Please try the below test class:
 
@isTest
public class sendMailToContactTriggerTest {
    static testmethod void testsendMailToContact(){
        
        // Add if there is any other required for Account creation
        Account acc = new Account(name='Test Account');
        insert acc;
        
        // Add if there is any other required for Contact creation
        Contact con = new Contact(LastName = 'Test Contact', Email = 'TestCon@gmail.com');
        
        Test.startTest();
        	insert con;
        Test.stopTest();
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
Vidya H 4Vidya H 4
@Maharajan C
thanks for your reply