You need to sign in to do that
Don't have an account?
BarryPlum
Help with Testing a Trigger
I wrote a trigger in my sandbox and it seems to work exactly as designed. I moved it over to production and, of course, I need a class to test the trigger. I created a class and got all the coverage I needed, but the test results aren't coming back as expected.
Here is the trigger code:
Here is the Testing Code:
I'm a relative newbie at Apex, so be gentle.
:smileywink:
Here is the trigger code:
Code:
trigger nullTaskType on Task (before insert,before update) { private Task[] newTask = Trigger.new; if (newTask[0].Type == null) { system.debug(newTask[0].subject); if (newTask[0].subject.contains('Email') || newTask[0].subject.contains('Brochure') ) { newTask[0].Type = 'Email'; } if (newTask[0].subject.contains('Web')) { newTask[0].Type = 'Web'; } } }
Here is the Testing Code:
Code:
public class nullTaskType { static testMethod void testNullTaskType() { Task t1 = new Task (subject='Email'); system.debug(t1.subject); system.debug(t1.type); Task t2 = new Task (subject='Brochure'); Task t3 = new Task (subject='Mass Email'); Task t4 = new Task (subject='Web'); Task t5 = new Task (subject='Something Else'); Task t6 = new Task (type='Email'); Task[] myTasks = new Task[] {t1,t2,t3,t4,t5,t6}; insert myTasks; system.debug(t1.type); system.debug(t2.type); system.debug(t3.type); system.debug(t4.type); system.debug(t5.type); // System.assertEquals('Email', t1.type); // System.assertEquals('Email', t2.type); // System.assertEquals('Email', t3.type); // System.assertEquals('Web', t4.type); System.assertEquals(null, t5.type); // System.assertEquals('Email', t6.type); } }
:smileywink:
Looks like your tests are failing because you can't reference the task type using dot notation form the inserted object.
You have to query for the inserted records then test the values.
Let us know if this was it.
Message Edited by mikef on 05-15-2008 04:32 PM
Is there any way to just look at the records I just temporarily inserted to see if they've been updated properly as opposed to querying the whole thing? Our Task object is huge.
I don't know if this will help with your particular situation, but here's some comments on the code you posted:
Bulk-Safe Trigger
Your Trigger does not appear to be written to handle Bulk Updates. If you are inserting multiple Tasks, your Trigger will be called once, with an array of Tasks in Trigger.new, yet you are only looking at newTask[0]. You should loop through the Trigger.new array and process each Task passed to the Trigger.
Trigger Test
I was writing my first Trigger Test yesterday and was continually getting failures until I realised that I had to retrieve the objects again after the Trigger fired. While the Id field was updated on my original objects, other fields weren't. So, try doing a SELECT to retrieve your inserted tasks (via t1.Id, t2.Id, etc) and test the type values then.
Good luck!
I'll have to figure out the select statements, thanks for all your help!
Barry
Message Edited by BarryPlum on 05-15-2008 10:42 PM
Here is my code:
Again, please forgive me if this is something I should know. I'm learning a LOT this week.
Barry
Thanks for your help.
For any other newbies out there, here is an example of a working Trigger with 90% test coverage.
First the Trigger code: