+ Start a Discussion
SFDC Coder 8SFDC Coder 8 

Test class for Task Trigger updating contact

Hi All,
I am new to Salesforce.
I have written a trigger on task to update contact record.
I want to write test class for the same. Here is my code.
trigger TaskUpdate on Task (after update) {
  List<Id> Ids=new List<Id>();
    for(Task t:trigger.new)
         {
        if(String.valueOf(t.whatId).startsWith('001')==TRUE)
          {
            Ids.add(t.whatId);
           }
      
         }
Map<Id,contact>  con = new Map<Id,contact>([SELECT Id, P1__c,p2__c,P3__c,AccountId FROM contact WHERE Contact.AccountId IN:Ids]);        
    For(Task t2 : trigger.new)
    {
        if(con.get(t2.whoid) != null)
        {  
           con.get(t2.whoid).P1__c = t2.P1__c ;
           con.get(t2.whoid).p2__c = t2.p2__c ;
           con.get(t2.whoid).P3__c = t2.P3__c ;
           con.get(t2.whoid).P4__c= t2.P4__c;
           con.get(t2.whoid).P5__c= t2.P5__c;
           con.get(t2.whoid).P6__c= t2.P6__c;
           con.get(t2.whoid).P7__c= t2.P7__c;
           con.get(t2.whoid).P8__c__c= t2.P8__c;
           
          }

    }
       update con.values();                     
}

Please help me to write test class for the above code.

Thanks in Advance
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest 
public class TaskUpdateTest 
{
    static testMethod void testMethod1() 
	{
		Account acc = new Account();
		acc.Name ='Test';
		insert acc;
		
		Contact cont = new Contact();
		cont.FirstName ='Test';
		cont.LastName ='testContact';
		cont.accountid =acc.id;
		insert cont;
		
	
		Task u = new Task();
			u.whatid = acc.id;
			u.whoid = cont.id;
			u.Subject='Run Test Trigger';
			u.Status='Not Started';
			u.Priority='Normal';  
		insert u;
		
	
		Test.startTest();
		
			update u;

		Test.stopTest();
		
	}
}

Let us know if this will help you