I'm trying to test a test a trigger that adds an error if the field Active__c is set to true on CreditInfo_Eval_Rules__c (son of CreditInfo_Fields__c) and the field Used__c is updated to false. I'm not sure how to create the test data.

This is the trigger:

trigger UsedTrigger on CreditInfo_Fields__c (before update) {
    List<CreditInfo_Eval_Rules__c> listCred = new List<CreditInfo_Eval_Rules__c>();

    listCred = [SELECT Id ,Active__c FROM CreditInfo_Eval_Rules__c WHERE CreditInfo_Fields__c IN : trigger.new];
    Set<ID> ids = trigger.newMap.keySet();

        for(Id newId : ids){
            if(trigger.newMap.get(newId).Used__c != trigger.oldMap.get(newId).Used__c && trigger.newMap.get(newId).Used__c==false   ){
                for(CreditInfo_Eval_Rules__c cred : listcred){
                    if(cred.Active__c == true){
                        trigger.newmap.get(newId).addError('The Credit Info Field is Active on a Rule!');

2 Answers 2


First of all, you will need to consider the relationship between the parent and child records. CreditInfo_Fields__c being the parent, CreditInfo_Eval_Rules__c being the child. We can now insert these objects into the database and not worry about the trigger running, as it is only on an update context, for now.

CreditInfo_Fields__c creditInfo = new CreditInfo_Fields__c(
    Used__c = true

CreditInfo_Eval_Rules__c creditInfoEval = new CreditInfo_Eval_Rules__c(
    CreditInfo_Fields__c = creditInfo.id,
    Active__c = true

Once you have the relationship created, you can go ahead and modify the CreditInfo_Fields__c field, Active__c. Before you cause the trigger to run with an update on the object, think about how you might report an exception.

creditInfo.Used__c = false;

    update creditInfo;
} catch (Exception e) {
    Boolean expectedExceptionThrown =  ex.getMessage().contains('The Credit Info Field is Active on a Rule!') ? true : false;
    System.assertEquals(expectedExceptionThrown, true);    

By simply reporting the exception is not a good enough test case. We use the System.assert() methods to confirm our understanding of the logic.

Consider looking into the following Trailhead modules:

  1. Apex Testing
  2. Apex Basics & Database
  3. Apex Triggers
  • Thank you for the help. I have now 66% code coverage!
    – I. Simoes
    Oct 2, 2018 at 10:18
  • 1
    @I.Simoes. Ahhh, code coverage. Quality unit testing will give you the coverage you need, focus on the test cases, not the coverage. Think about the happy & unhappy code paths with the above example I have given you.
    – TSmith
    Oct 2, 2018 at 11:18
  • This exception testing paradigm is ill advised. If no exception is thrown at all, your recommended test will pass.
    – Adrian Larson
    Oct 18, 2018 at 20:25

You will need to create the records for objects, CreditInfo_Fields__c and CreditInfo_Eval_Rules__c.

CreditInfo_Fields__c infoFields = new CreditInfo_Fields__c();
infoFields.Active__c = true;
infoFields.<OtherFields> = <valueforField>;

insert infoFields;

CreditInfo_Eval_Rules__c evalRules = new CreditInfo_Eval_Rules__c();
evalRules.Active = true;
evalRules.<OtherFields> = <valueforField>;

insert evalRules;

After this you need to update the infoFields record and your trigger should be fired.

update infoFields;

You can a few debug statements in your trigger to trace the flow and set the right values of the fields.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .