0

I've got a simple trigger written to update attributes of a child record when the value of the attribute in the related parent changes. The trigger works fine for actions triggered in the UI, but it doesn't seem to fire properly in my test class.

I've got 2 objects, GoalManagement__c and SalesGoals__c. Each SalesGoal__c record is related to 1 parent record in GoalManagement. Both objects have attributes to track quarter goals, Q1__c, Q2__c, etc. The GoalManagement trigger is intended to update the quarterly goals of a SalesGoal__c if an update is made to the parent GoalManagement__c record.

Trigger code:

trigger GoalManagementTrigger on GoalManagement__c (after update) {

    for(GoalManagement__c gm: Trigger.new){

        if(Trigger.isAfter && Trigger.isUpdate) {
        System.debug('Goal management trigger: ' +gm);

        //Select child sales goal record
        SalesGoals__c sg = [Select Id, Q1__c,Q2__c,Q3__c,Q4__c from SalesGoals__c where ParentGoal__c = :gm.Id];

        System.debug('sales goal: '+ sg);

        sg.Q1__c = gm.Q1__c;
        sg.Q2__c = gm.Q2__c;
        sg.Q3__c = gm.Q3__c;
        sg.Q4__c = gm.Q4__c;

        //Update child records with value from parent
        update sg;        
        }
    }

}

Test code:

    @isTest
public class GoalManagementTriggerTest {

    static testMethod void validateGoalManagementTrigger () {

        //Get a user ID
        User smith;
        smith = [Select Id from User where LastName='Smith'];
        System.debug('Smith: '+ smith.id);

        //Create a parent goal
        GoalManagement__c parentGoal = new GoalManagement__c(
                                                            Owner2__c=smith.id,
                                                            Parent__c='National',
                                                            Name__c='John Smith',
                                                            Q1__c=100000,
                                                            Q2__c=200000,
                                                            Q3__c=300000,
                                                            Q4__c=400000,
                                                            Year__c='2018');

        insert parentGoal;
        System.debug('Parent Goal - insert: '+parentGoal);

        SalesGoals__c childGoal = new SalesGoals__c(ParentGoal__c=parentGoal.id,
                                                    Q1__c=100000,
                                                    Q2__c=200000,
                                                    Q3__c=300000,
                                                    Q4__c=400000,
                                                    Year__c='2018',
                                                    User__c=smith.id
                                                );

        insert childGoal;

        System.debug('Child Goal - insert: '+childGoal);

        parentGoal.Q1__c = 1;

        update parentGoal;

        System.debug(parentGoal);

        System.debug('child goal after update: '+childGoal);


        System.assertEquals(1,childGoal.Q1__c);

    }

}
  • 2
    You need to query back for any updates... – Adrian Larson Sep 18 '17 at 17:57
3

You need to query back for any updates.

Test.startTest();
    // do some stuff
Test.stopTest();

Child__c updatedChild = [SELECT UpdatedField__c FROM Child__c WHERE Id = :childId];
system.assertEquals(updatedValue, updateChild.UpdatedField__c, 'Meaningful assertion');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.