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
Patrick G. BrownPatrick G. Brown 

Null Pointer Exception - Only on Bulk Insert

I have a trigger that looks to see if the lookup field AVSFQB__Primary_Contact__c (looks up to Contact) has changed on an Opportunity and if so, deletes all Contact Roles and adds the value from the AVSFQB__Primary_Contact__c field.

Everything works perfectly except when I try to fire the trigger via DataLoader.  I've handled null pointer issues before, but in this example, I'm checking for null on both the list of Opportunities as well as the field AVSFQB__Primary_Contact__c just to be sure.  When executing via the ui, it works great.  When trying to upload a set of records it fails.  

Can someone point me in the right direction?
 
trigger opportunityMaintenance on Opportunity (after insert, after update) {

    if(Trigger.isAfter){
        if(Trigger.isUpdate || Trigger.isInsert){
            set<Id> setOpportunityIds = new set<Id>() ;
            for(Opportunity opp : Trigger.new){
                if(opp != null){
                    //Error on the next line: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object
                    if((opp.AVSFQB__Primary_Contact__c != null) && (Trigger.oldMap.get(opp.Id).AVSFQB__Primary_Contact__c != Trigger.newMap.get(opp.Id).AVSFQB__Primary_Contact__c))
                    {
                    setOpportunityIds.add(opp.Id) ;
                    }
                }    
            }
            if(!setOpportunityIds.isEmpty())
            delete([SELECT Id from opportunityContactRole WHERE OpportunityID IN: setOpportunityIds]) ;

        List<Opportunity> oppList = new List<Opportunity>();
        oppList = [select Id, Name, AVSFQB__Primary_Contact__c from Opportunity where Id in :Trigger.New];
            for(Opportunity opp : oppList) {
                if (oppList.size()>0){
                    if(opp.AVSFQB__Primary_Contact__c != NULL){
                        opportunityContactRole ocr = new opportunityContactRole(
                        ContactId = opp.AVSFQB__Primary_Contact__c,
                        OpportunityId = opp.Id,
                        Role = 'Decision Maker',
                        IsPrimary = TRUE);
                        insert ocr;    
                    }    
                }
            }
        }   
    }
}

 
Satish PrajapatSatish Prajapat
Hello Patrick G. Brown,
Please write if condition before below code:
if((opp.AVSFQB__Primary_Contact__c != null) && (Trigger.oldMap.get(opp.Id).AVSFQB__Primary_Contact__c != Trigger.newMap.get(opp.Id).AVSFQB__Primary_Contact__c))
                    {
                        setOpportunityIds.add(opp.Id) ;
                    }

the condition should to be like :
if(Trigger.oldMap.size() != 0) ///This is the logic that you to applied here.
{

if((opp.AVSFQB__Primary_Contact__c != null) && (Trigger.oldMap.get(opp.Id).AVSFQB__Primary_Contact__c != Trigger.newMap.get(opp.Id).AVSFQB__Primary_Contact__c))
                    {
                        setOpportunityIds.add(opp.Id) ;
                    }
}

Choose it best answer if it helps you.
Thanks,
Satish kumar Prajapat.
Deepak Pandey 13Deepak Pandey 13
Hi Patrick,
Please share the exception line which is showng null.

try this - 
trigger opportunityMaintenance on Opportunity (after insert, after update) {

    if(Trigger.isAfter){
        if(Trigger.isUpdate){
            set<Id> setOpportunityIds = new set<Id>() ;
            for(Opportunity opp : Trigger.new){
                if(opp != null){
                    //Error on the next line: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object
                    if(opp.AVSFQB__Primary_Contact__c != null && Trigger.oldMap.get(opp.Id).AVSFQB__Primary_Contact__c != opp.AVSFQB__Primary_Contact__c)
                    {
                    setOpportunityIds.add(opp.Id) ;
                    }
                }    
            }
            if(setOpportunityIds != null && !setOpportunityIds.isEmpty())
                delete([SELECT Id from opportunityContactRole WHERE OpportunityID IN: setOpportunityIds]) ;
        }

         if(Trigger.isinsert){
        List<Opportunity> oppList = new List<Opportunity>();
        opportunityContactRole ListOCR= new opportunityContactRole();
        oppList = [select Id, Name, AVSFQB__Primary_Contact__c from Opportunity where Id in :Trigger.New];
        if (oppList.size()>0){
            for(Opportunity opp : oppList) {
                    if(opp.AVSFQB__Primary_Contact__c != NULL){
                        opportunityContactRole ocr = new opportunityContactRole(
                        ContactId = opp.AVSFQB__Primary_Contact__c,
                        OpportunityId = opp.Id,
                        Role = 'Decision Maker',
                        IsPrimary = TRUE);
                        ListOCR.add(ocr); 
                    }    
                }
            }
            if(ListOCR.size()>0)
                insert ListOCR;  
         }
    }
}