You need to sign in to do that
Don't have an account?
AlSawtooth
NullPointerException on trigger.newmap.keyset()
I have a trigger that's calling from a class, and I'm getting a NullPointerException on the first line of the 3rd method (List<Contact> constoupdate = [select Id, FACheckbox__c from Contact where Id in :Trigger.newMap.keySet()];). Anyone know why this is happening?
(Also, I know I probably have a recursion problem here, which I'm hoping to solve with a static variable - but one issue at a time!)
Trigger:
Class:
(Also, I know I probably have a recursion problem here, which I'm hoping to solve with a static variable - but one issue at a time!)
Trigger:
trigger TestTrigger on Contact (before insert, before update, after insert, after update) { if(Trigger.isBefore) { TriggerClass.MethodBeforeT(); } else if(Trigger.isAfter) { TriggerClass.MethodAfterT(); } TriggerClass.MethodLast(); }
Class:
public with sharing class TriggerClass { public static void MethodBeforeT (){ List<RecordType> type1 = [select Id from RecordType where Name = 'Type 1' and SobjectType = 'Contact' limit 1]; List <RecordType> type2 = [select Id from RecordType where Name = 'Type 2' and SobjectType = 'Contact' limit 1]; for (Contact cons :(List <Contact>) Trigger.new){ if(cons.Checkbox__c = true && cons.RecordTypeId == type1[0].Id){ cons.RecordTypeId = type2[0].Id;} } } public static void MethodAfterT (){ List<CustomE__c> customes = new List <CustomE__c>(); List<Contact> constoupdate = [select Id, FirstName, LastName, Email, Checkbox__c from Contact where Id in :Trigger.newMap.keySet()]; List<Contact> constoupdate2 = new List <Contact>(); for (Contact c :constoupdate){ if(c.Checkbox__c = true){ CustomE__c e = new CustomE__c(); e.Contact__c = c.Id; e.First_Name__c = c.FirstName; e.Last_Name__c = c.LastName; e.Email__c = c.Email; customes.add(e); constoupdate2.add(c); }} insert customes; } public static void MethodLast (){ List<Contact> constoupdate = [select Id, Checkbox__c from Contact where Id in :Trigger.newMap.keySet()]; List<Contact> constoupdate2 = new List <Contact>(); for(Contact c :constoupdate){ c.Checkbox__c = false; constoupdate2.add(c);} update constoupdate2; } }
So change your method signature to ... MethodAfterT(set<Id> contactIds), and change your query to use :contactIds instead of trigger.newmap.keyset().
and change your trigger code to ... TriggerClass.MethodAfterT(trigger.newmap.keyset());