You need to sign in to do that
Don't have an account?
José Teixeira Gomes
Need help with a test class for this trigger
Trigger FollowUpTask on Task(after update) { Set<Id> completedTasks = new Set<Id>(); Set<Id> contactIds = new Set<Id>(); Set<Id> accountIds = new Set<Id>(); for (Task task: Trigger.new) { if (task.Status != Trigger.oldMap.get (task.Id).Status && task.Status=='Completed') completedTasks.add(task.Id); contactIds.add(task.WhoId); accountIds.add(task.WhatId); } contactIds.remove(null); accountIds.remove(null); Map<Id, Contact> contactMap = new Map<Id, Contact>(); Map<Id, Account> accountMap = new Map<Id, Account>(); if (!contactIds.isEmpty()) { contactMap = new Map<Id, Contact>([ select Contact_Frequency__c, Name from Contact where Id in :contactIds ]); } if (!accountIds.isEmpty()) { accountMap = new Map<Id, Account>([ select Primary_Contact__c, Name, Tier__c from Account where Id in :accountIds ]); } List<Task> newTasks = new List<Task>(); for (Id id: completedTasks) { Task task = Trigger.newMap.get(id); Contact contact = contactMap.get(task.WhoId); Account account = accountMap.get(task.WhatId); if (account == null || contact == null) { continue; } Task newTask = new Task( Subject = 'Follow Up ' + account.Name + '/' + contact.Name, OwnerId = account.OwnerId, WhoId = account.Primary_Contact__c, WhatId = account.Id, Priority = (account.Tier__c == 'Tier 1') ? 'High' : 'Normal' ); Date duedate = system.today().addDays((Integer)(contact.Contact_Frequency__c)); newTask.ActivityDate = dueDate; newTasks.add(newTask); } if (!newTasks.isEmpty()) { insert newTasks; } }
NOTE: This code has not been tested and may contain typographical or logical errors
All Answers
-
I would suggest you start with a simple postive test that updates a task to completed and then fetches the tasks for that account/contact and checks to see that your new task was inserted.
-
Then create a negative test that updates a task, but doesn't mark it complete and make sure no new tasks are completed.
-
Then create a bulk update test that has a mix of newly completed task as well as tasks that are just being updated.
For an example of this type of testing, see this post [1]. It is directled at leads but should give you enough information to get started on for your tasks[1] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
NOTE: This code has not been tested and may contain typographical or logical errors
But I am not sure how to do the assertion part with lists. Also, don't I need to create like 50 accounts, contacts, etc.?
and use result for all of your asserts.
If you move your inserts up (I suggest directly under your creation of the object like in my example) it should work.
Again, thank you very much for your help. One last thing, despite the code coverage I still get the x of failed in the developer console. Why is that?
[1] https://success.salesforce.com/issues_view?id=a1p30000000T0FeAAK