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
AkshuAkshu 

How to write test class for trigger when task and email created at a time

************************trigger***************************
trigger TaskandemailusingTrigger on Account (after insert) {
    List<Task> tasklist= new List<task>();
    Set<User> ownerID = new Set<User>();
 
 user u=[select Name, Email from User where id=:userinfo.getUserId()];
  
    for (Account acc:trigger.new){
        if(acc.type=='Prospect'){
            task t=new task();
            t.whatid=acc.id;
            t.Priority='normal';
            t.OwnerId=u.Id;
            t.Status='not started';
            t.subject='Account task created'+acc.Name;
        
            tasklist.add(t);
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      String[] toAddresses = new String[] {u.Email};
        mail.setToAddresses(toAddresses); 
       mail.setSubject('your task has been created'); 
            mail.setHtmlBody('this is Acoount task mail');
        Messaging.Email[] emails=new Messaging.Email[]{mail};
        Messaging.sendEmail(emails);
       }
    }
        
        insert tasklist;
        
        
        
    }
Suraj Tripathi 47Suraj Tripathi 47
Hi,
Please follow the below code:-
@istest
public class AccountTriggerTest {

    @istest
    public static void testAcc(){
          Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
        
        Account a = new account();
        a.name='Test';
        a.Type='Prospect';
        insert a;

    }
}

Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi

 
CharuDuttCharuDutt
Hii Akshu
Try Below Code
@istest
public class AccountTriggerTest {

    @istest
    public static void testAcc(){

        Account acc = new account();
        acc.name='Test';
        acc.Type='Prospect';
        insert acc ;

    }
}
Please mark it as Best Answer if it helps
Thank you.