You need to sign in to do that
Don't have an account?

Trigger Test class not working: Trigger on Task to update Contact custom field
Hi, I've created a trigger on a task that I want to use to update a custom contact field. I found documentation on how to create the trigger (http://salesforce.stackexchange.com/questions/23055/trigger-to-update-custom-field-from-another-custom-field-on-a-separate-object). However, When I run the test class I get an Assertion Failed Error: System.AssertException: Assertion Failed: Expected: 2014-05-29 00:00:00, Actual:
null.
I am unsure of where my error lies and hoping someone can help.
Trigger Code:
trigger UpdateInTouchDate on Task (after insert, after update) {
Set<String> whoIds = new Set<String>();
for (Task t : Trigger.new) {
whoIds.add(t.WhoId);
}
List<Contact> cons = [SELECT Id, In_Touch_Date__c FROM Contact WHERE Id =: whoIds];
Map<String, Task> taskMap = new Map<String, Task>();
for (Task t : Trigger.new){
taskMap.put(t.WhoId, t);
}
for (Contact c : cons) {
if (taskMap.containsKey(c.Id)) {
c.In_Touch_Date__c = taskMap.get(c.Id).ActivityDate;
}
}
update cons;
}
Trigger Test Class:
@isTest
private class TestUpdateTouchDate {
static testMethod void testUpdateInTouch() {
Contact c = new Contact(FirstName = 'Test', LastName = 'Test');
insert c;
Date d = Date.newInstance(2014, 5, 29);
Task t = new Task(ActivityDate = d);
System.debug('Date before updating in touch date: ' + t.ActivityDate );
insert t;
List<Task> task = [SELECT ActivityDate FROM Task WHERE Id =:t.Id];
if (task.size() > 0) {
t = task.get(0);
}
System.debug('Price after trigger fired: ' + t.ActivityDate);
System.assertEquals(t.ActivityDate, c.In_Touch_Date__c);
}
}
null.
I am unsure of where my error lies and hoping someone can help.
Trigger Code:
trigger UpdateInTouchDate on Task (after insert, after update) {
Set<String> whoIds = new Set<String>();
for (Task t : Trigger.new) {
whoIds.add(t.WhoId);
}
List<Contact> cons = [SELECT Id, In_Touch_Date__c FROM Contact WHERE Id =: whoIds];
Map<String, Task> taskMap = new Map<String, Task>();
for (Task t : Trigger.new){
taskMap.put(t.WhoId, t);
}
for (Contact c : cons) {
if (taskMap.containsKey(c.Id)) {
c.In_Touch_Date__c = taskMap.get(c.Id).ActivityDate;
}
}
update cons;
}
Trigger Test Class:
@isTest
private class TestUpdateTouchDate {
static testMethod void testUpdateInTouch() {
Contact c = new Contact(FirstName = 'Test', LastName = 'Test');
insert c;
Date d = Date.newInstance(2014, 5, 29);
Task t = new Task(ActivityDate = d);
System.debug('Date before updating in touch date: ' + t.ActivityDate );
insert t;
List<Task> task = [SELECT ActivityDate FROM Task WHERE Id =:t.Id];
if (task.size() > 0) {
t = task.get(0);
}
System.debug('Price after trigger fired: ' + t.ActivityDate);
System.assertEquals(t.ActivityDate, c.In_Touch_Date__c);
}
}
If this helps,please mark this as best answer to help others :)
All Answers
If this helps,please mark this as best answer to help others :)
@isTest
private class TestUpdateTouchDate {
static testMethod void testUpdateInTouch() {
Contact c = new Contact(FirstName = 'Test', LastName = 'Test');
insert c;
Date d = Date.newInstance(2014, 5, 29);
Task t = new Task(ActivityDate = d, WhoId=c.id, Status='Not Started',Priority='Normal'); //Associating with contact
System.debug('Date before updating in touch date: ' + t.ActivityDate );
insert t;
// Query on Contact as the Trigger is populating Contact field
List<Contact> con = [SELECT In_Touch_Date__c FROM Contact WHERE Id =:c.Id limit 1];
if (con.size() > 0) {
c = con.get(0);
}
System.debug('Price after trigger fired: ' + t.ActivityDate);
System.assertEquals(t.ActivityDate, con[0].In_Touch_Date__c);
}
}