+ Start a Discussion
BrittanieBrittanie 

How to write a trigger test on field update from contact to task

I wrote a trigger that works in sandbox but have to now write a trigger test (class) for it and although I am learning through doing, I am officially lost.  please help

 

Trigger TaskBefore on Task(before insert, before update){
   
   
    Map<Id, List<Task>> whoIdsMap = new Map<Id, List<Task>>();
    
    for(Task t : trigger.new){
       
        if(t.WhoId != null){
            
            if(!whoIdsMap.containsKey(t.WhoId)){
                
                List<Task> temp = new List<Task>();
                
                temp.add(t);
               
                whoIdsMap.put(t.WhoId, temp);
            }else{
                
                whoIdsMap.get(t.WhoId).add(t);
            }
        }
    }
   
    
    for(Contact con : [Select Id, Name,Title from Contact where Id in :whoIdsMap.keySet()]){
        
        for(Task t :whoIdsMap.get(con.Id)){
            t.Contacts_Title__c = con.Title;
        }
    }
}

 

Avidev9Avidev9

You have to do something like this

 

@istest
private class MyTestClass{

  static testMethod void myUnitTest(){
  	Contact con = new Contact(
      							Lastname = 'Test'
                             	Title = 'Mr'
                             ); // fill in all other required fields fields
    insert con;
    Task taskObj = new Task(
    							WhoId = con.Id
    						);	// fill in all other required fields fields
    
    Test.startTest();
    	insert taskObj;
        //verify if the trigger did the job  
System.assertEquals(Contact.Title,[SELECT Contacts_Title__c FROM Task WHERE Id=:taskObj.Id].Contacts_Title__c);
Test.stopTest(); } }

 I just created skeleton code for you, you can use the same to create your test class

BrittanieBrittanie

I'm not sure what you mean by required fields... the only required field on the contact is "Lastname."  I am getting the following error:

 

Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, String at line 17 column

9

Please advise

Avidev9Avidev9
Oops my mistake!

System.assertEquals(Contact.Title,[SELECT Contacts_Title__c FROM Task WHERE
Id=:taskObj.Id].Contacts_Title__c);

Replace this with

System.assertEquals(Con.Title,[SELECT Contacts_Title__c FROM Task WHERE
Id=:taskObj.Id].Contacts_Title__c);

Well this line is just for checking if your trigger is working as expected.
You can comment this line as well to avoid error.
BrittanieBrittanie
@istest
private class MyTestClass{

  static testMethod void myUnitTest(){
    Contact con = new Contact(
            Lastname = 'Test'
            ); // fill in all other required fields fields
   
   insert con;
    Task taskObj = new Task(
            WhoId = con.Id);
            Subject = 'CVM';
            Status = 'In Progress';
            Priority = 'High';
            Owner = 'Name';}
              // fill in all other required fields fields
    
    Test.startTest();}
        insert taskObj;
        //verify if the trigger did the job  
        System.assertEquals(Con.Title,[SELECT Contacts_Title__c FROM Task WHERE
Id=:taskObj.Id].Contacts_Title__c);      
    Test.stopTest();
  }                             
}

 is current code now, but now recieving the following error: (@ insert taskObj;

Error: Compile Error: unexpected token: insert at line 19 column 8

Avidev9Avidev9
You seemed to have changed a lot of syntax. for example

new Task(
WhoId = con.Id);
Subject = 'CVM';
Status = 'In Progress';
Priority = 'High';
Owner = 'Name';}

This is not correct

it should be

new Task(
WhoId = con.Id,
Subject = 'CVM',
Status = 'In Progress',
Priority = 'High',
Owner = 'Name');


Another one Test.startTest();}

it should be Test.startTest();