0

I built this error to fire off an exception when an account's SLA field with open cases is being changed, when it shouldn't.

Trigger

trigger DoNotAllowSlaChangesWithOpenCases on Account (before update) {

//Find the account's open cases
List<Account> accountsWithCases = [SELECT Id,
                                        (SELECT Id 
                                        FROM Cases 
                                         WHERE isClosed = false 
                                         LIMIT 1) 
                                   FROM Account 
                                   WHERE Id IN :Trigger.New];

//Check if the account's SLA field is changed
for (Account acc : accountsWithCases) {
    String oldSlaValue = Trigger.oldMap.get(acc.id).SLA__c;
    String newSlaValue = Trigger.newMap.get(acc.id).SLA__c;
    Boolean isAccountSlaChanged = oldSlaValue != newSlaValue;
    //Check Open Cases
    Boolean accHasOpenCases = acc.Cases.Size() > 0;
    //Get the Trigger.new version of the account
    Account accInTriggerNew = trigger.newMap.get(acc.Id);
   //If SLA is changed and there are open cases
    if(isAccountSlaChanged && accHasOpenCases){
       accInTriggerNew.SLA__c.addError('Nope, not today baby!');
    }

}
}

Test Class

@isTest
private class testDoNotAllowChangesWithOpenCases {

static testMethod void runaTest() {

    Account acc = new Account();
    acc.Name = 'Test Class';
    acc.SLA__c = 'Gold';
    insert acc;

    Case c = new Case();
    c.Origin = 'Phone';
    c.Status = 'New';
    c.Priority = 'Medium';
    insert c;

    try {
       acc.SLA__c = 'Platium';
    update acc;
    }catch(Exception e){
        System.assert(e.getMessage().contains('Nope, not today baby!'));
    }
    test.stopTest();

}

static testMethod void runaTest2() {
    Account acc = new Account();
    acc.Name = 'Test Class';
    acc.SLA__c = 'Platinum';
    insert acc;

    Case c = new Case();
    c.Origin = 'Phone';
    c.Status = 'New';
    c.Priority = 'Medium';
    insert c;

    acc.SLA__c = 'Platium';
    update acc;

    test.startTest();        
    try {
        acc.SLA__c = 'Platium';
    update acc;
    }catch(Exception e){
        System.assert(e.getMessage().contains('Nope, not today baby!'));
    }
    test.stopTest();
    }}

I am able to reach 88% code coverage with all of my code covered but can't seem to get the addError to be covered, are there any steps I should take.

1 Answer 1

2

You neglected to assign the AccountId to the case before inserting it:

...
insert acc;
...
c.AccountId = acc.Id;
...

By the way, in order to avoid regression bugs, you should include a false assertion:

try {
    acc.SLA__c = 'Platium';
    update acc;
    System.assert(false, 'Expected to receive an exception.');
}catch(Exception e){
    System.assert(e.getMessage().contains('Nope, not today baby!'));
}

The false assertion will make sure that the exception is thrown or the test will fail.

You must log in to answer this question.

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