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
MaggieSumitMaggieSumit 

Getting NullPointerException oppTr: execution of AfterInsert caused by: System.NullPointerException

  1. trigger oppTr on Opportunity (after update,after insert){
  2.         
  3.     Map<Id,Opportunity> OppStage = new Map<Id,Opportunity>();
  4.     List<Account> AccList = new List<Account>();
  5.     
  6.     for(Opportunity opp : trigger.new){        
  7.         if(opp.StageName == 'Closed Lost' && trigger.oldMap.get(opp.Id).StageName == 'Closed Won'){
  8.            OppStage.put(opp.AccountId, opp);
  9.         }
  10.     }
  11.     System.debug('OppStage'+OppStage);
  12.    try {            
  13.    if(OppStage.size()>0){
  14.        System.debug('OppStage'+OppStage.size());
  15.         for(Opportunity opp2 : OppStage.values()){
  16.            System.debug('opp2'+opp2);
  17.            Account acc = new Account();
  18.            acc.Id = opp2.AccountId;
  19.            acc.Status__c = 'Lost';
  20.            AccList.add(acc);
  21.            System.debug('AccList'+acc.Status__c);
  22.         } 
  23.        update AccList;
  24.        System.debug('AccList'+AccList);
  25.     } 
  26.    } catch (System.NullPointerException e){
  27.      String s;
  28.         s.toLowerCase();
  29.    }   
  30.  
  31. }
Best Answer chosen by MaggieSumit
kirubakaran viswanathankirubakaran viswanathan
Hi Sumit,

The null pointer exception caused because Trigger.oldmap wont work with Insert trigger. So please update your code as below
trigger oppTr on Opportunity (after update,after insert){
        
    Map<Id,Opportunity> OppStage = new Map<Id,Opportunity>();
    List<Account> AccList = new List<Account>();
    
    if(Trigger.isUpdate)
    {
    for(Opportunity opp : trigger.new){        
        if(opp.StageName == 'Closed Lost' && trigger.oldMap.get(opp.Id).StageName == 'Closed Won'){
           OppStage.put(opp.AccountId, opp);
        }
    }
    System.debug('OppStage'+OppStage);
    }
    
    if(Trigger.isInsert)
    {
        for(Opportunity opp : trigger.new){        
        if(opp.StageName == 'Closed Lost'){
           OppStage.put(opp.AccountId, opp);
        }
    }
    System.debug('OppStage'+OppStage);
    }
   try {            
   if(OppStage.size()>0){
       System.debug('OppStage'+OppStage.size());
        for(Opportunity opp2 : OppStage.values()){
           System.debug('opp2'+opp2);
           Account acc = new Account();
           acc.Id = opp2.AccountId;
           acc.description = 'Lost';
           AccList.add(acc);
           System.debug('AccList'+acc.description);
        } 
       update AccList;
       System.debug('AccList'+AccList);
    } 
   } catch (System.NullPointerException e){
     String s;
        s.toLowerCase();
   }   
 
}

Please mark this answer as best answer if it resolve your problem

All Answers

kirubakaran viswanathankirubakaran viswanathan
Hi Sumit,

The null pointer exception caused because Trigger.oldmap wont work with Insert trigger. So please update your code as below
trigger oppTr on Opportunity (after update,after insert){
        
    Map<Id,Opportunity> OppStage = new Map<Id,Opportunity>();
    List<Account> AccList = new List<Account>();
    
    if(Trigger.isUpdate)
    {
    for(Opportunity opp : trigger.new){        
        if(opp.StageName == 'Closed Lost' && trigger.oldMap.get(opp.Id).StageName == 'Closed Won'){
           OppStage.put(opp.AccountId, opp);
        }
    }
    System.debug('OppStage'+OppStage);
    }
    
    if(Trigger.isInsert)
    {
        for(Opportunity opp : trigger.new){        
        if(opp.StageName == 'Closed Lost'){
           OppStage.put(opp.AccountId, opp);
        }
    }
    System.debug('OppStage'+OppStage);
    }
   try {            
   if(OppStage.size()>0){
       System.debug('OppStage'+OppStage.size());
        for(Opportunity opp2 : OppStage.values()){
           System.debug('opp2'+opp2);
           Account acc = new Account();
           acc.Id = opp2.AccountId;
           acc.description = 'Lost';
           AccList.add(acc);
           System.debug('AccList'+acc.description);
        } 
       update AccList;
       System.debug('AccList'+AccList);
    } 
   } catch (System.NullPointerException e){
     String s;
        s.toLowerCase();
   }   
 
}

Please mark this answer as best answer if it resolve your problem
This was selected as the best answer
MaggieSumitMaggieSumit
Thanks a lots @kirubakaran viswanathan this code is only working for insert operation not for update.
kirubakaran viswanathankirubakaran viswanathan
Hi Sumit,

It is working good for update also. I tested it. Please make sure you are changing the "Closed Won" opportunity to "Closed Lost". Do not change the oppor to "Closed Lost" from other stage.

Thanks,
Kiruba. V
MaggieSumitMaggieSumit
Thanks, @kirubakaran viswanathan, but I want to change the opp to "Closed Lost" from another stage. Then what I need to do ?
but thanks a lost it helps a lot. I mark as best answer.
kirubakaran viswanathankirubakaran viswanathan
Then it is very simple, you dont need any isUpdate or isInsert. use
for(Opportunity opp : trigger.new){        
        if(opp.StageName == 'Closed Lost'){
            Account acc = new Account();
           acc.Id = opp2.AccountId;
           acc.description = 'Lost';
           AccList.add(acc);
           
        }
    }
        update AccList;

This will solve

 
MaggieSumitMaggieSumit
Thanks, @kirubakaran viswanathan, but I want to check old record first, if there is any 'Closed Won' opp on an account, and then a more recent 'Closed Lost' opp on the account, the Account Status should update to 'Lost'

So, as per my requirement, your first code is correct, but the only thing needed to change like If second opp stage is any value and its change to "Closed Lost". then it will update the account status.

Thanks a lots. I will manage that.