0

I have written a trigger which will fire email messaging. Can anyone help me out how to write a test class for this.

trigger taskEmail on Project__c (after update ) {
    set<String> projName = new set<String>();
    set<String> projName1 = new set<String>();
    list<Task__c> listTask = new list<Task__c>();

    for(Project__c pro : Trigger.new) {
        projName.add(pro.Name);
    }


    listTask = [SELECT Id,Project_Approval__c,Project_Name__c,Status__c,Task_Executioner__c FROM Task__c WHERE Project_Name__c IN : projName];
    system.debug('11111' + listTask.size());
    EmailTemplate et=[Select id from EmailTemplate where name='Multiple Tasks'];
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();

    if(listTask.size() > 0) {
    system.debug('222222' + listTask.size());
    Set<Id> executionerIds = new Set<Id>();
        for(Task__c con : listTask ) {
        system.debug('33333  enter for ');       
        if(con.Project_Approval__c =='Approved' && con.Status__c == 'Open' && !executionerIds.contains(con.Task_Executioner__c)){
            system.debug('444   enter if inside for ');

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setTargetObjectId(con.Task_Executioner__c);
            mail.setTemplateId(et.Id);
            mail.setSaveAsActivity(false);
            mail.setWhatId(con.Id);
            mails.add(mail);
            executionerIds.add(con.Task_Executioner__c);

            //Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
        system.debug('0000000');

    }

    Messaging.sendEmail(mails);
}
}

Regards

1

1 Answer 1

1

While writing a test class you do not need to write test code for email messages. You just need to create your dummy records for "Project" and "Task" object as per your trigger criteria , and then insert them in the test class. This will cover your whole trigger.

You can refer the below link if you are new to test classes https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

Regards! Ruchi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .