• Fra6
  • NEWBIE
  • 10 Points
  • Member since 2019
  • Salesforce Idontknow

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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
  • May 13, 2019
  • Like
  • 0
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
  • May 13, 2019
  • Like
  • 0