You need to sign in to do that
Don't have an account?
Fra6
After trigger not running on test class
Hey all, I have an after trigger that runs fine and everything updates correctly. But I'm not sure why on my test class it's not running when I systemAssert at the end.
public class OpportunityHelper {
public static void updateDistributions(map<Id, Opportunity> newMap, map<Id, Opportunity> oldMap){
for(Id opportunityID : newMap.keyset()){
//get donations with their related distributions
List<Opportunity> distributionsID = [
SELECT (SELECT Id FROM DistributionDonation__r)
FROM Opportunity
WHERE Id = :opportunityID
];
//check if donation date changed
if(oldMap.get(opportunityID).CloseDate != newMap.get(opportunityID).CloseDate){
Date newDate = newMap.get(opportunityID).CloseDate;
//updated all related distributions
for(Opportunity Opp : distributionsID) {
for(Product_Distribution__c distribution : Opp.DistributionDonation__r){
distribution.Date_TBD__c = newDate;
}
update Opp.DistributionDonation__r;
}
}
}
}
@isTest
public class OpportunityHelperTest {
static testMethod void testDateChange() {
Test.startTest();
Product_Distribution__c insertedTest = TestHelper.createRecords();
Date newDate = Date.newInstance(2001, 01, 01);
Opportunity opp = [SELECT ID, CloseDate From Opportunity Where ID =: insertedTest.Product_Donation_Name__c];
opp.CloseDate = newDate;
Test.stopTest();
Product_Distribution__c dis = [Select ID, Product_Donation_Name__c, Date_TBD__c From Product_Distribution__c Where ID =: insertedTest.Id];
System.assertEquals(opp.CloseDate, dis.Date_TBD__c);
}
}
It always ends up not updating and instead just gets the date that was initalized with.
System.AssertException: Assertion Failed: Expected: 2001-01-01 00:00:00, Actual: 2019-05-13 00:00:00
public class OpportunityHelper {
public static void updateDistributions(map<Id, Opportunity> newMap, map<Id, Opportunity> oldMap){
for(Id opportunityID : newMap.keyset()){
//get donations with their related distributions
List<Opportunity> distributionsID = [
SELECT (SELECT Id FROM DistributionDonation__r)
FROM Opportunity
WHERE Id = :opportunityID
];
//check if donation date changed
if(oldMap.get(opportunityID).CloseDate != newMap.get(opportunityID).CloseDate){
Date newDate = newMap.get(opportunityID).CloseDate;
//updated all related distributions
for(Opportunity Opp : distributionsID) {
for(Product_Distribution__c distribution : Opp.DistributionDonation__r){
distribution.Date_TBD__c = newDate;
}
update Opp.DistributionDonation__r;
}
}
}
}
@isTest
public class OpportunityHelperTest {
static testMethod void testDateChange() {
Test.startTest();
Product_Distribution__c insertedTest = TestHelper.createRecords();
Date newDate = Date.newInstance(2001, 01, 01);
Opportunity opp = [SELECT ID, CloseDate From Opportunity Where ID =: insertedTest.Product_Donation_Name__c];
opp.CloseDate = newDate;
Test.stopTest();
Product_Distribution__c dis = [Select ID, Product_Donation_Name__c, Date_TBD__c From Product_Distribution__c Where ID =: insertedTest.Id];
System.assertEquals(opp.CloseDate, dis.Date_TBD__c);
}
}
It always ends up not updating and instead just gets the date that was initalized with.
System.AssertException: Assertion Failed: Expected: 2001-01-01 00:00:00, Actual: 2019-05-13 00:00:00
All Answers
opp.CloseDate = newDate;
Update opp;
Thanks ,
Maharajan.C
System.AssertException: Assertion Failed: Expected: 2001-01-01 00:00:00, Actual: 2019-05-13 00:00:00