function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
skiptomylou11skiptomylou11 

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;
    
    }

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

In this section:

 

 for (Bidder_Contractors__c item : Trigger.new)
        OpportunityIds.add(item.Opportunity__c);
 

 you are assuming trigger.new is always available.  However, for a delete it won't be.  You should defend against this, for example:

 

if (trigger.isInsert)
{
   for (Bidder_Contractors__c item : Trigger.new)
        OpportunityIds.add(item.Opportunity__c);
}

 

 

 

All Answers

bob_buzzardbob_buzzard

In this section:

 

 for (Bidder_Contractors__c item : Trigger.new)
        OpportunityIds.add(item.Opportunity__c);
 

 you are assuming trigger.new is always available.  However, for a delete it won't be.  You should defend against this, for example:

 

if (trigger.isInsert)
{
   for (Bidder_Contractors__c item : Trigger.new)
        OpportunityIds.add(item.Opportunity__c);
}

 

 

 

This was selected as the best answer
skiptomylou11skiptomylou11

Thanks Bob!!!

 

Your solution works perfectly!

niklasenniklasen
Hi,
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.
 
trigger tskUpdatetrigger on Case (after insert, after update,after delete) {
    List<Case> lstCase = [select id, ContactId from Case where id in: trigger.newmap.keyset()];
    set<Id> sAccId = new set<Id>();

    for(Case cs: lstCase){
        if(cs.ContactId != null){
            sAccId.add(cs.ContactId);
        }
    }
    if(sAccId != null && sAccId.size() > 0){
        List<Contact> lstContact = [select id, Cases__c, (select id from Cases) from Contact where id in: sAccId];
        if(lstContact.size() > 0){
            for(Contact acc: lstContact){
                acc.Cases__c = acc.Cases.size();
            }
                      update lstContact;
            }
    }
}

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 
 
if (trigger.isInsert || trigger.isUpdate) {
      update lstContact;
}

but no success.
Anyone who could help me solving this issue?