1

I have this trigger that errors out at line 13 NullPointerException attempt to de-reference a null object in Apex Trigger Code

trigger Event_Attendee_vod on Event_Attendee_vod__c (before update, before insert) {
Set<String> accountIds = new Set<String>();
Set<String> contactIds = new Set<String>();
Set<String> userIds = new Set<String>();
VOD_Utils.setTriggerEventAttendee(true);
Map<Id, Event_Attendee_vod__c> eventAttendeeMap = new Map<Id, Event_Attendee_vod__c>([SELECT Id, Account_vod__c, Account_vod__r.Name, Account_vod__r.Formatted_Name_vod__c,
                                                                                        Account_vod__r.FirstName, Account_vod__r.LastName, Contact_vod__c, Contact_vod__r.Name,
                                                                                        Contact_vod__r.FirstName, Contact_vod__r.LastName, User_vod__c, User_vod__r.Name,
                                                                                        User_vod__r.FirstName, User_vod__r.LastName FROM Event_Attendee_vod__c WHERE Id IN : Trigger.new]);
for (Integer i=0; i < Trigger.new.size(); i++) {
    String attendeeName = '';
    Event_Attendee_vod__c attendee = eventAttendeeMap.get(Trigger.new[i].Id);
    if (attendee.Account_vod__c != null) {
        if (attendee.Account_vod__r.Formatted_Name_vod__c != null) {
            attendeeName = attendee.Account_vod__r.Formatted_Name_vod__c;
        } else if (attendee.Account_vod__r.LastName != null && attendee.Account_vod__r.FirstName != null) {
            attendeeName = attendee.Account_vod__r.LastName + ', ' + attendee.Account_vod__r.FirstName;
        } else {
            attendeeName = attendee.Account_vod__r.Name;
        }
    } else if (attendee.Contact_vod__c != null) {
        if (attendee.Contact_vod__r.LastName != null && attendee.Contact_vod__r.FirstName != null) {
            attendeeName = attendee.Contact_vod__r.LastName + ', ' + attendee.Contact_vod__r.FirstName;
        } else {
            attendeeName = attendee.Contact_vod__r.Name;
        }
    } else if (attendee.User_vod__c != null) {
        if (attendee.User_vod__r.LastName != null && attendee.User_vod__r.FirstName != null) {
            attendeeName = attendee.User_vod__r.LastName + ', ' + attendee.User_vod__r.FirstName;
        } else {
            attendeeName = attendee.User_vod__r.Name;
        }
    }
    if (attendeeName != '') {
        Trigger.new[i].Attendee_vod__c = attendeeName;
    }
5
  • Is line 13 if (attendee.Account_vod__c != null) {?
    – BarCotter
    Jul 22, 2015 at 14:41
  • yes, i missed to point it out.
    – user59759
    Jul 22, 2015 at 14:46
  • 1
    Heres some things you can do to debug the issue: 1: Figure out if the trigger is firing on a before or after event. 2: Check eventAttendeeMap to make sure there are records in it. 3: Check that Trigger.new[i].Id is the value you were expecting and that eventAttendeeMap has that as a key.
    – BarCotter
    Jul 22, 2015 at 14:48
  • 1
    The error means that attendee is null on line 13.
    – BarCotter
    Jul 22, 2015 at 14:51
  • all these nested if-else make my head spin -- the logic should be moved into a reusable trigger handler class method where you pass the fields that you need to test
    – cropredy
    Jul 22, 2015 at 17:52

1 Answer 1

5

Your trigger can't work with "before insert", because you can't query for records that haven't been assigned an Id, which won't happen until "after insert". You'll probably want to start of like this:

trigger X on Y (after insert, before update) {
    if(Trigger.isInsert) {
        update Trigger.new.deepClone(true);
        return;
    }
    // Rest of original logic here

This way you'll call a recursive update after insert, which is legal.

Alternatively, you can simply make some maps, query your records, and then use those values directly. E.g.

Set<Id> accountVodIds = new Set<id>();
for(Event_Attendee_vod__c record: Trigger.new) {
    accountVodIds.add(record.Account_vod__c);
}
Map<Id, Account> accountVod = new Map<Id, Account>([SELECT ... FROM Account WHERE Id IN :accountVodIds]);
for(Event_Attendee_vod__c record: Trigger.new) {
    if(accountVod.containsKey(record.Account_Vod__c)) {
        // We found the account
    }
}

You'll need to do this for each of the three fields.

0

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.