You need to sign in to do that
Don't have an account?

AfterDelete caused by: System.NullPointerException: Attempt to de-reference a null object:
Hello,
We have a custom object named "Bidder & Contractors" which is linked to Opportunities via a Lookup relationship.
As we want to have Roll-up summary functionality on the opportunity counting the 'Bidders & Contractors' set to 'Won' we put in the below trigger.
It is working fine until you try to delete a 'Bidder & Contractor' entry, which gives the following error:
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger OpportunityRollUpContractors caused an unexpected exception, contact your administrator: OpportunityRollUpContractors: execution of AfterDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.OpportunityRollUpContractors: line 6, column 1".
Many thanks for your help!
trigger OpportunityRollUpContractors on Bidder_Contractors__c (after delete, after insert, after update) { Set<id> OpportunityIds = new Set<id>(); List<Opportunity> opportunitiesToUpdate = new List<Opportunity>(); for (Bidder_Contractors__c item : Trigger.new) OpportunityIds.add(item.Opportunity__c); if (Trigger.isUpdate || Trigger.isDelete) { for (Bidder_Contractors__c item : Trigger.old) OpportunityIds.add(item.Opportunity__c); } // get a map of the shipments with the number of items Map<id,Opportunity> opportunityMap = new Map<id,Opportunity>([select id, Contractor_Count__c from Opportunity where id IN :OpportunityIds]); // query the Opportunities and the related Competition items and add the size of the Competition items to the Opportunity's Contractor_Count__c for (Opportunity opp: [select Id, Name, Contractor_Count__c,(select id from Awarded_Contractors_Bidders__r WHERE Awarded__c ='Won') from Opportunity where Id IN :OpportunityIds]) { opportunityMap.get(opp.Id).Contractor_Count__c = opp.Awarded_Contractors_Bidders__r.size(); // add the value/shipment in the map to a list so we can update it opportunitiesToUpdate.add(opportunityMap.get(opp.Id)); } update opportunitiesToUpdate; }
In this section:
you are assuming trigger.new is always available. However, for a delete it won't be. You should defend against this, for example:
All Answers
In this section:
you are assuming trigger.new is always available. However, for a delete it won't be. You should defend against this, for example:
Thanks Bob!!!
Your solution works perfectly!
I am tackling with the same Problem.
I have a trigger running on cases to count how many open cases are in the system for a contact.
Having this trigger in place, I cannot delete a case anymore with that above error message, skiptomylou11 had earlier on his problem.
I tried adding
but no success.
Anyone who could help me solving this issue?