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

Help: Trigger not Working
Hi,
This is my first post on the board and am hoping someone can help!
I created a trigger to populate a custom Task field (Opportunity_Close_Date__c) with the Opportunity Close Date.
Below please find my trigger and the test class I wrote.
The problem is that I'm getting 100% code coverage but the test is failing (Error Message: System.AssertException: Assertion Failed).
Any ideas or help would be greatly appreciated!!
Thanks!
Here's the trigger:
trigger OppCloseDate on Opportunity (after insert, after update) { Set<Id> oppIds = new Set<Id>(); Set<Id> taskIds = new Set<Id>();{ for(Task t:[select Id, WhatID from Task]){ String wId = t.WhatId; if(wId!=null && wId.startsWith('006')){ taskids.add(t.WhatId); for(Opportunity to:[select Id from Opportunity where Id in :taskIds]){ oppIds.add(to.ID); } Set<Id> oppstoupdate = new Set<Id>(); for(Integer i=0;i<trigger.new.size();i++){ if(oppIds.contains(trigger.new[i].Id)){ oppstoupdate.add(Trigger.new[i].id); if(oppstoupdate.size() > 0){ List<Opportunity> oList = [select Id, CloseDate from Opportunity where Id in :oppstoupdate]; List<Task> taskstoupdate = new List<Task>();{ for(Task tsk : [select Id, Opportunity_Close_Date__c from Task where WhatId in :oppstoupdate]){ tsk.Opportunity_Close_Date__c = oList[0].CloseDate; taskstoupdate.add(tsk); } } if(!taskstoupdate.isEmpty()){ update taskstoupdate; } } } } } } } }
And here's the test class:
@isTest private class Test_OppCloseDate { public static testMethod void myUnitTest(){ List<Opportunity> opp1 = new List<Opportunity>{ new Opportunity( Name = 'Test Opp1', StageName = 'Closed Won', Type = 'New Business', CloseDate = Date.today()+7, Demo_ID__c = NULL, LicenseType__c = 'Enterprise')}; insert opp1; List<Opportunity> opp2 = new List<Opportunity>{ new Opportunity( Name = 'Test Opp2', StageName = 'Closed Won', Type = 'New Business', CloseDate = Date.today()+2, LicenseType__c = 'Enterprise')}; insert opp2; List<Task> tsk1 = new List<Task>{ new Task( WhatId = opp1[0].id, Subject = 'Task 2', Status = 'Not Started', Priority = 'Normal', Type = 'Demonstration - In Person')}; insert tsk1; List<Task> tsk2 = new List<Task>{ new Task( WhatId = NULL, Subject = 'Task 2', Status = 'Not Started', Priority = 'Normal', Type = 'Demonstration - Web/Phone')}; insert tsk2; List<Opportunity> oppstoupdate1 = New List<Opportunity>{ [select id from Opportunity where id in :opp1]}; for(Opportunity oOP:oppstoupdate1) oOP.CloseDate = Date.today(); Update oppstoupdate1; List<Task> taskstoupdate2 = New List<Task>{ [select id from task where id in :tsk2]}; for(task tOK:taskstoupdate2) tOK.WhatId = opp2[0].id; Update taskstoupdate2; Task [] tasks1 = [select ID, Opportunity_Close_Date__c from Task where ID in :tsk1]; System.assert(tasks1[0].Opportunity_Close_Date__c == (opp1[0].CloseDate)); Task [] tasks2 = [select ID, Opportunity_Close_Date__c from Task where ID in :tsk2]; System.assert(tasks2[0].Opportunity_Close_Date__c == (opp2[0].CloseDate)); } }
All Answers
Looks like the Opportunity_Close_Date__c and ClosedDate do not match up. add system.debug before the tasks are saved, take a look at the acutal values
Try this...
Thanks so much for your help, namo!
I thought there was a problem with my actual trigger and it turns out I didn't have the class set up correctly.
Such a huge help!!!!