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

please Write a test class for trigger(after update) below Code please help me

trigger  EmailAlert on Task (after Update) {

for(Task m :
if (m.Mail_Send_Check__c == True ) {
//User p = [SELECT Id,Email FROM User WHERE UserRole = 'Team lead/Supervisor'];
List <User> p = [SELECT Id,Email,Name FROM User WHERE UserRoleId IN(select id from UserRole where Name = 'Team lead/Supervisor')];
  Set<string> users = new Set<string>();
  List<Task> taskList = new List<Task>();
  for(Integer i = 0; i< p.size(); i++)
    //for(string requireduser :users)  
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
     Task tt = new Task();
 //tt.Ownerid = P[0].id;
  tt.Ownerid = p[i].id;
  tt.Status = 'Not Started';
  tt.ActivityDate =;
  tt.Subject = 'Alert Task';
 insert taskList;





I can give you skeleton for writing the test method and can suggest what you need to do.

First go through below links which will give you basic understanding about writing test methods.


public class testemailalert
 static testmethod void testemailalertmethod()
   // here create a user data for test method as suitable for ur main class query
   //next create list of task with some test data and insert it.
   //it will covere the trigger some part,now check the part which is not covered and write test data to covere that part also
   List<Profile> userProfile = new List<Profile>([SELECT Id FROM profile WHERE name='System Administrator' ]);
   List<Role> userrole = new List<role>([select id from UserRole where Name = 'Team lead/Supervisor']);
   //data for user object
   User usr = new User(alias = 'standt', email = '', 
                                  emailencodingkey = 'UTF-8', lastname = 'TestLasName', languagelocalekey = 'en_US', 
                                  localesidkey = 'en_US', profileid = userProfile.get(0).id ,
                                  timezonesidkey = 'America/Los_Angeles', UserRoleId=userrole..get(0).id , username = '');
                                  insert usr;
	 //Test Data for the task object
     Task t = new Task(subject='test', ActivityDate=Date.Today(),Status='Not Started',Priority='Normal',Mail_Send_Check__c= true);
     insert t;
	 // here if needed insert list of tasks


 I didn't check the above code in my org, it may contain some syntax error or need to update some test data.

Try to write the more test data if required with the information provided above.


thank's for giving solution


it will Came Code coverage 0%(Zero) yar.

can u please check it your organization


it's very important 


thanks Hari


for giving Good Suggestion


Now I got it.