You need to sign in to do that
Don't have an account?
José Teixeira Gomes
First Test Class
Hello, I am very new to Salesforce and after having successfully written a trigger, with a great deal of help from the forum, I am having some issues with the test class. I thought it would be enough to test an example of where the trigger would be fired and see if the trigger did the job but apparently it is not, as I only got 73% of the lines tested. So I wanted your feedback on what I did wrong, any help is appreciated.
The trigger, the issues seem to be with the maps and towards the end:
And this is the test class, I am sure there are a ton of errors:
The trigger, the issues seem to be with the maps and towards the end:
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; } }
And this is the test class, I am sure there are a ton of errors:
@istest public class FollowUpTaskTest { static testmethod void testFollowUpTask () { Contact Ctc = new Contact (LastName = 'TestContact', Contact_Frequency__C = 30); Account Acc = new Account (Name = 'TestAccount', Rank__C = 3, Primary_Contact__C = Ctc.Id, OwnerId = '00520000003j1B2'); Date Today = System.today(); Task Tsk = new Task ( Subject = 'TestTask', OwnerId = '00520000003j1B2', WhoId = Ctc.Id, WhatId = Acc.Id, Priority = 'Normal', Status = 'Not Started', ActivityDate = Today.addDays(5) ); insert Acc; insert Ctc; insert Tsk; Test.startTest(); Tsk.Status = 'Completed'; Update Tsk; Test.stopTest(); Tsk = [select Id, Subject, OwnerId, WhatId, WhoId, Priority, Status, ActivityDate from Task where WhatId = :Acc.Id and WhoId = :Ctc.Id and ActivityDate = :Today.addDays((integer)(Ctc.Contact_Frequency__C)) ]; System.assert(Tsk.ActivityDate == Today.addDays((integer)(Ctc.Contact_Frequency__C))); System.assert(Tsk.OwnerId == Acc.OwnerId); System.assert(Tsk.Subject == 'Follow Up ' + Acc.Name + '/' + Ctc.Name); System.assert(Tsk.WhoId == Acc.Primary_Contact__c); System.assert(Tsk.WhatId == Acc.Id); System.assert(Tsk.Priority =='High'); } }
you can use Developer Console to run your tests, and then if you open up the class or trigger from the test results,
you will see the lines that are covered and the ones that are not covered highlighted in different colours..
you can then fine tune your test class to cover the missing part..
refer these links for how to run test classes and check code coverage in Developer Conosle
https://help.salesforce.com/HTViewHelpDoc?id=code_dev_console_tab_tests.htm&language=en_US
https://help.salesforce.com/HTViewHelpDoc?id=code_dev_console_tests_coverage.htm&language=en_US
I have done exactly that, unfortunately I do not have enough skill to know how to correct the test to cover those lines. Is there a way to post the results of the test? As I said, the problem seems to be the mapping and the end of the trigger.