What you'd need to do is write a test class that Inserts and/or Updates a Task record, making sure the Subject is equal to "face to face".
You're also running queries in this trying to find the account and contact related to the task, so you'd need to create those in the test class.
@isTest
private class myTriggerTest{
@isTest static void testInsert() {
Account a = new Account(
Name = 'Universal Containers'
);
INSERT a;
Contact c = new Contact(
Name = 'Foo Bar',
AccountId = a.Id
);
INSERT c;
Task t = new Task(
Subject = 'face to face',
AccountId = a.Id,
WhoId = c.Id,
ActivityDate = Date.today()
);
INSERT t;
}
}
Literally all this does is performs 2 Insert operations creating an Account & Contact. This is data you'll need for your Task.
Now we perform another Insert operation for Task, assigning the AccountId
and WhoId
to the Account and Contact we've just created and the name to face to face.
- Your trigger will always execute on insert of a task, so that'll be covered.
- It'll then try to get into the
if
statement. The condition of your if
statement requires the Subject
to be "face to face", so we've met that.
- You're running two queries within that getting the Account and Contact based off of what you've inserted in the Task. That data will exist now because we created it! So they should be covered.
- Finally, you're running two
if
statements, checking if acc
and con
have data. They should do because that's what we did before. You're then updating the relevant Account and Contacts fields for Last_Face_to_Face_Meeting__c
, setting them to the Activity Date.
What you should do following this is run some asserts. In other words, writing what you expect to see and checking what you actually get. For instance:
Date today = Date.today();
Integer i = [SELECT COUNT() FROM Account WHERE Last_Face_to_Face_Meeting__c = :today];
System.assertEquals(1, i);
Here we're simply checking that there should be 1 Account that exists where the field Last_Face_to_Face_Meeting__c
has a date for today. Because you're not using seeAllData=true
, there is only 1 account to check for and it should have that value, if the Trigger worked.
Hope this helps or points you in the right direction.