Sorry, this is probably a really simple question, but I'm struggling to wrap my head around writing a test for my trigger which will allow me to get the required amount of code coverage to push into production. I work best from examples, but I can't quite find what I'm looking for, so I'm here asking for help!

My trigger is:

trigger Task_FaceToFace on Task (after insert,after update) {
for (Task t: Trigger.New){
        if(t.subject.IndexOf('face to face') != -1)
            List  acc = [SELECT Account.Name,Account.Site FROM Account WHERE Id =: t.AccountId LIMIT 1];
            List  con = [Select Id,Name FROM Contact WHERE ID =: t.WhoId LIMIT 1];

            if(acc != null && !acc.IsEmpty())
                acc[0].Last_Face_to_Face_Meeting__c = t.ActivityDate;    
                update acc[0];
            if(con != null && !con.IsEmpty())
                con[0].Last_Face_to_Face_Meeting__c = t.ActivityDate;
                update con[0];    



Basically I'm firing it on a new task creation, looking at the subject of the task, if it contains face to face, it then updates a custom field in both the account and contact.

Any ideas greatly appreciated!

  • What test code have you tried yet? What lines are you having trouble covering? Commented Jan 27, 2016 at 15:25
  • The thing is, I've never written a test before, so this is me learning! Any help or suggestions appreciated Commented Jan 27, 2016 at 15:35

1 Answer 1


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.

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.

  • That's fantastic! Thanks so much for your help, it's so much clearer now! Commented Jan 27, 2016 at 15:57
  • @PeterRossi Literally think of test classes in two parts: coverage and assertions. Coverage is simply the execution of code. If you have an if statement and don't match the criteria required to execute the. The code in there won't be covered. Assertions take two parameters: what you expect to happen and what actually happens. If you have a function that adds a and b, and you put a = 2 and b = 2, you'd expect 4 right? But if that function produced 5, you'd have a problem. Just because it's covered, doesn't mean it's tested. :)
    – Dan Jones
    Commented Jan 27, 2016 at 16:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .