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
BALA_RAMBALA_RAM 

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

trigger  EmailAlert on Task (after Update) {

for(Task m : trigger.new)
{
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++)
   {
      users.add(p[i].id);
   //}    
    //for(string requireduser :users)  
   //{ 
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     mail.setTargetObjectId(p[i].Id);
     mail.setTemplateId('00XF0000001P4ll');
     mail.saveAsActivity=false;
     //mail.add(mail);
     Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
     //Messaging.sendEmail(mail);
     Task tt = new Task();
 //tt.Ownerid = P[0].id;
  tt.Ownerid = p[i].id;
  tt.Status = 'Not Started';
  tt.ActivityDate = date.today();
  tt.Subject = 'Alert Task';
  taskList.add(tt);
   }
 
 insert taskList;
  }
 }
}

HariDineshHariDinesh

 

Hi,

 

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.

 

http://teachmesalesforce.wordpress.com/2011/05/07/how-to-write-a-trigger-test/

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_test.htm

 

@isTest
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 = 'test@9876test.com', 
                                  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 = 'testUniqudeName@test.com');
                                  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.

BALA_RAMBALA_RAM

thank's for giving solution

but

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

can u please check it your organization

 

it's very important 

BALA_RAMBALA_RAM

thanks Hari

 

for giving Good Suggestion

 

Now I got it.