+ Start a Discussion
Nadia GainsbourgNadia Gainsbourg 

Test class for a basic Task trigger

Hello! I have a basic Task trigger that I need to move to production; however, I have no idea how to write a test class for it (don't understand test classes at all). Would appreciate any help on this! 

trigger TaskTrigger on Task (before insert) {

   if ((Trigger.isBefore && Trigger.isInsert)) {
     for(Task t : Trigger.new)    {
          if(String.isNotBlank(string.valueOf(t.WhoId)) && (String.valueOf(t.WhoId)).startsWith('003') ){
                    // contact
                    t.Contact_Phone_Hidden_Lookup__c = t.WhoId;
                }
                else if(String.isNotBlank(string.valueOf(t.WhoId)) && String.valueOf(t.WhoId).startsWith('00Q') ){
                    t.Lead_Phone_Hidden_Lookup__c= t.WhoId;
                }
     }
    }
}
Raj VakatiRaj Vakati
@isTest
public class TaskTriggerTest 
{
static testMethod void testStandardUserwithTask() 
{
Profile standardProf = [select id from profile where name='Standard 
						 User'];
User su = new User(alias = 'standt',         
email='standarduser@azugatask.com',emailencodingkey='UTF- 
							 8',FirstName='task',

	lastname='testing',languagelocalekey='en_US',localesidkey='en_US',profileid = standardProf.Id,
				   timezonesidkey='America/Los_Angeles',username='standarduser@azugatask.com');
System.runAs(su) 
{
	Contact a = new Contact(LastName='Testtask',Email='test@gmail.com');
	insert a;
	 
		Task t = new Task(Subject='Donni'+i,Status='New',Priority='Normal',CallType='Outbound' ,WhoId=a.Id);
	 
	 
	 Lead l = new Lead(LastName='Testtask',Email='test@gmail.com');
	insert l;
	 
		Task t1 = new Task(Subject='Donni'+i,Status='New',Priority='Normal',CallType='Outbound' ,WhoId=l.Id);
	test.startTest();
	 insert t;
	  insert t1;
	test.stopTest();
	 
}

}


}