I'm getting the following error when trying to run my test class:
System.DmlException: Delete failed. First exception on row 0 with id a0m55000000iJQuAAM; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, RemoveCheckboxOnContact: execution of AfterDelete caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.RemoveCheckboxOnContact: line 19, column 1: []
Here is the trigger I'm testing:
trigger RemoveCheckboxOnContact on emma__Emma_Group_Member__c (after delete) {
//Make a list of all the deleted Group Member records
List<emma__Emma_Group_Member__c> deletedMembers = Trigger.new;
//Make a list of all the Contact IDs in those records
List<ID> contactIds = new List<ID>();
List<emma__Emma_Group__c> hopaGeneralList = [SELECT Id FROM emma__Emma_Group__c WHERE Name = 'Hopa General' LIMIT 1];
System.debug('hopaGeneral List' + hopaGeneralList);
emma__Emma_Group__c hopaGeneral = hopaGeneralList.get(0);
System.debug('hopaGeneralId = ' + hopaGeneral.Id);
//Add the Contact IDs from the Emma Group Member deleted from the Hopa General Group to the list
if(hopaGeneral.Id != NULL && deletedMembers.size() > 0){
For(emma__Emma_Group_Member__c groupMember : deletedMembers){
System.debug('Emma Group ID: ' + groupMember.Id);
if(groupMember.emma__Emma_Group__c == hopageneral.Id){
String contactId = groupMember.emma__Contact__c;
contactIds.add(contactId);
}
}
}
//Uncheck the Hopa General checkbox for every Contact whose ID is on the list we just created.
//Then, add them to a list of contact to be updated
List<Contact> contactsToUpdate = new List<Contact>();
if (contactIds.size() > 0){
For(Contact c : [SELECT Id, Hopa_General__c
FROM Contact
WHERE Id IN : contactIDs]){
c.Hopa_General__c = False;
contactsToUpdate.add(c);
}
}
if (contactsToUpdate.size() > 0){
//Update the contacts
Update contactsToUpdate;
}
}
And here is the test class:
@isTest
public class RemoveCheckboxOnContactTriggerTest {
static testMethod void testCheckbox(){
// Create new contact
Contact c = new Contact();
c.FirstName = 'Test';
c.LastName = 'Contact';
insert c;
System.debug('Contact created: ' + c.Id);
// Create Hopa General Emma Group ID
emma__Emma_Group__c g = new emma__Emma_Group__c();
g.Name = 'Hopa General';
insert g;
System.debug('Hopa General Group created: ' + g.Id);
// Create Hopa General Group Membership for Contact
emma__Emma_Group_Member__c e = new emma__Emma_Group_Member__c();
e.emma__Contact__c = c.Id;
e.emma__Emma_Group__c = g.Id;
insert e;
System.debug('Emma Group Member created: ' + e.Id);
// Delete Hopa General Group Membership
delete e;
// Verify Hopa General checkbox on Contact is false
system.debug('Contact Hopa General Checkbox: ' + c.Hopa_General__c);
}
}
The error is referencing this line:
if(hopaGeneral.Id != NULL && deletedMembers.size() > 0) {
Is there something wrong with my syntax here?