3

I'm trying to update a boolean field on Case called Boo__c when a new Case Comment is added to the Case and it contains the word boo.

trigger OHT on CaseComment (before insert,before update) {

    for(CaseComment c : Trigger.New) {
            if(c.CommentBody.contains('boo'))
                c.Parent.boo__c=true;

    }
}

I'm getting the null pointer exception:

Apex trigger OHT caused an unexpected exception, contact your administrator: OHT: execution of BeforeInsert caused by:

System.NullPointerException: Attempt to de-reference a null object: ()

the field boo__c is on the Case object and I have access to it (I checked the Field Level Security)

What am I missing?

4

You can't just update related records in this way. You need to actually call update on a record. You can't access parent fields in a trigger context, but in this case you do not even need a query because you just need the ParentId.

Map<Id, Case> cases = new Map<Id, Case>();
for (CaseComment record : trigger.new)
    if (record.ParentId != null)
        cases.put(record.ParentId, new Case(Id=record.ParentId, Boo__c = true));
update cases.values();

What's going on above?

  • Take a collections based approach. You never want to perform queries or DML Operations in a loop.
  • In this case, two CaseComment records could have the same Case parent. Since you would get an error for trying to update the same record twice in a list, use a Map<Id, Case>.
  • You can update a record without querying for it if you know its Id. In this case, you're basically updating new Case(Id=caseId, CheckboxField__c=true).
  • You need to actually update the records. You need to work with a List<SObject> when making your update call, hence the use of values().

If you want to filter on RecordType as well, you will indeed need to query. In this case, you will want to first collect the ParentId values that should be updated.

Set<Id> booCaseIds = new Set<Id>();
for (CaseComment comment : trigger.new)
    if (comment.CommentBody != null && comment.CommentBody.containsIgnoreCase('boo'))
        booCaseIds.add(comment.ParentId);
List<Case> records = [
    SELECT Id FROM Case WHERE Id IN :booCaseIds
    AND RecordType.DeveloperName = 'Some_Specific_RecordType'
];
for (Case record : records) record.Boo__c = true;
update records;

You should also look at An Introduction to Exception Handling and add proper exception handling to your code.

  • Hi Adrian, thanks for the tip I actually didn't know that you can't reference related child/parent on a Trigger. How can I also check if the parent (i.e. case) has recordTypeId='01220000000FffjAAC' ? In that case I need to query the case like you mentioned, right? – Json Nov 16 '16 at 14:58
  • Yes, then you need to query. I can update in a moment. – Adrian Larson Nov 16 '16 at 14:59
  • yes please do that would be great – Json Nov 16 '16 at 15:10

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.