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
SFDev@321SFDev@321 

TRIGGER :- System.NullPointerException: Attempt to de-reference a null object

REQUIREMENT :- Need to be able to delete Opportunities via data loader. 
i.e when i'm trying to delete some opportunites through DATA LOADER i'm getting the below ERROR :-


Getting the following message: 
tgrOpportunityBefore: execution of BeforeDelete 
caused by: System.NullPointerException: Attempt to de-reference a null object 
Trigger.tgrOpportunityBefore: line 379, column 1 

 

 

 

TRIGGER :-

 

 // --- This is used since most of the triggers are for Insert and Update on Opportunity.
    if(trigger.isInsert || trigger.isUpdate) {  
      //#101OICD - Variable Declaration -- Start    
      StaticVariables.setAccountDontRun(true); // stop account trigger from running
      List<Id> accIds = new List<Id>();
      Set<id> accidset = new set<id>();
      //#101OICD - Variable Declaration -- End
      
   
 
      //#101OICD -- Loop Through Opp records  and assign values -- Start
      for(Opportunity o:Trigger.new) {
        
        system.debug('Originator EIN ' + o.Originator_EIN__c);
        allCampaignIds.add(o.CampaignId);//CR2180        
        //if(o.Opportunity_Owner_EIN__c == '802558891') o.ABR_Count__c =1;  
        
     
        
        if(o.CloseDate ==null)o.CloseDate = (Date.today()+ 365);
        
        if(o.AccountId != null && o.Auto_Assign_Owner__c)accIds.add(o.accountId);
        
        if(o.OwnerId != null)o.owner__c = o.OwnerId;
        
        //add flag for org to org
        if (
        (o.Routing_Product__c == 'Engage IT' || o.Product_Family__c == 'ENGAGE IT') 
        && o.CreatedDate >= (date.newinstance(2011, 11, 19))  
        //    || ((trigger.newmap.get(o.id).Routing_Product__c != trigger.oldmap.get(o.id).Routing_Product__c)
        //    || (trigger.newmap.get(o.id).Product_Family__c != trigger.oldmap.get(o.id).Product_Family__c ))
        ){
            o.s2s_Link__c = 'BTB to Engage IT';  
        }
        

    //#101OICD -- Part of 101OICD Trigger to Modify Opportunit Owner By Account -- End
     
     //#101OICD -- Part of 101OICD Trigger to Modify Account Information -- Start
        //Added by GS to update account last call date.
        //Update the account last call date field for these accounts      
        list<account> updateCallDate = new list<account>();          
        updateAccountLastContacted updateAccountCallDate = new updateAccountLastContacted();                     
        //updateCallDate = updateAccountCallDate.updateAccount(accidset);
        String objName='Opportunities';
        updateCallDate = updateAccountCallDate.updateAccount(accidset, objName);           
        update updateCallDate;               
    //#101OICD -- Part of 101OICD Trigger to Modify Account Information -- End
    }
     
    //Set RAG Status to None if Leasing History has 0 records -- Start
   LINE 379 :- for(Opportunity o : trigger.new){
    Integer i = [SELECT count() FROM Leasing_History__c WHERE Opportunity__c =: o.Id LIMIT 1];
    if(i < 1){
      o.Finance_Available__c = 'None';
     }
    }    
    //Set RAG Status to None if Leasing History has 0 records -- End   
}

Best Answer chosen by Admin (Salesforce Developers) 
Gunners_23Gunners_23

In delete context Trigger.New is not available thats the reason its throwing error.

 

consider if you want to run a block of code based on delete then insert the logic as shown below

 

 if(Trigger.isDelete)

{

    if(Trigger.IsBefore || Trigger.IsAfter) // depends on criteria

    {

               // Your logic

    }

}

 

In this case the whole trigger wont run incase of deletion of record only specified block will run

All Answers

stcforcestcforce

trigger.old? I don't think delete triggers have a trigger.new

SFDev@321SFDev@321

no actually this trigger is not for deleting opportunity......Actually this trigger is creating a problem while i'm trying to delete opportunities fron dataloader....

Gunners_23Gunners_23

Is it a complete code?? where is the block for running trigger based on delete criteria.

 

Trigger.New is available only for "Insert" and "Update" for more info pls do check the link below

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

SFDev@321SFDev@321

Thanks for your reply.....this trigger is inserting some data in opportunity......my question is...trigger.new part is making problem in deleting opportunities...so what should i do...so this should not make problem while deleting opportunity from trigger...

Gunners_23Gunners_23

In delete context Trigger.New is not available thats the reason its throwing error.

 

consider if you want to run a block of code based on delete then insert the logic as shown below

 

 if(Trigger.isDelete)

{

    if(Trigger.IsBefore || Trigger.IsAfter) // depends on criteria

    {

               // Your logic

    }

}

 

In this case the whole trigger wont run incase of deletion of record only specified block will run

This was selected as the best answer