+ Start a Discussion
Kiran kumar 193Kiran kumar 193 

Need urgent help on this issue

My requirement was Latest opportunity name should be displayed in account object custom field.

Getting null pointer exception when I delete the opportunity for the Account. How do we handle this error.

Class :

public class OppAccUpdate
{
  
  public void oppupdate(List<opportunity> oplist)
  {
      
    set<id> accid = new set<id>();
    
   // List<Account> acctoUpdate= new List<Account>();
   
   Map<id,Account> mapaccounts = new Map<id,Account>();
    
    for(opportunity opp:oplist)
    {
       accid.add(opp.Accountid);
    }
    List<Account> acc =[select id, oppname__c,(select id, name from opportunities order by createddate) from Account where id in:accid];
    system.debug('****Thesize of account***'+acc.size());
    system.debug('****The oppname***'+acc[0].oppname__c);
    system.debug('****The opportunity***'+acc[0].opportunities);
    for(Account a:acc)
    {
       for(opportunity o:a.Opportunities)
       {
          if(a.oppname__c==null || (a.oppname__c!=o.Name))
          {
          system.debug('***inside for loop***'+ a.oppname__c);
          system.debug('***inside for loop111***'+ o.name);
          a.oppname__c = o.Name;
          mapaccounts.put(a.id,a);
         // acctoUpdate.add(a);
          }
       }
    }
    if(!mapaccounts.isEmpty())
    update  mapaccounts.values();

      
  }
}

Trigger:

trigger oppaccupdate on Opportunity (after insert, After update, After delete) 
{
     Opportunity[] opp = trigger.new;
     
     if(trigger.isInsert || trigger.isAfter)
     {
       OppAccUpdate ou = new OppAccUpdate();
       ou.oppupdate(opp);
     }
     
      if(trigger.isUpdate || trigger.isAfter)
     {
       OppAccUpdate ou = new OppAccUpdate();
       ou.oppupdate(opp);
     }
     
      if(trigger.isDelete || trigger.isAfter)
     {
       Opportunity[] opp1 = trigger.old;
       OppAccUpdate ou = new OppAccUpdate();
       ou.oppupdate(opp1);
     }
}
Boris BachovskiBoris Bachovski
Which line are you getting the null-pointer exception at?
SaranSaran
Hi Kiran,

The issue is with the trigger i think,
 
trigger oppaccupdate on Opportunity (after insert, After update, After delete) 
{
     
     if(trigger.isInsert || trigger.isAfter)
     {
         Opportunity[] opp = trigger.new;
         OppAccUpdate ou = new OppAccUpdate();
         ou.oppupdate(opp);
     }
     
      if(trigger.isUpdate || trigger.isAfter)
     {
         Opportunity[] opp = trigger.new;
         OppAccUpdate ou = new OppAccUpdate();
         ou.oppupdate(opp);
     }
     
      if(trigger.isDelete || trigger.isAfter)
     {
         Opportunity[] opp1 = trigger.old;
         OppAccUpdate ou = new OppAccUpdate();
         ou.oppupdate(opp1);
     }
}

Hope this might help you 

Thanks!!
Arunkumar RArunkumar R
Hi Kiran,

    Please take a look on the below link. This will very helpful to your problem.

http://salesforcekings.blogspot.in/2015/08/how-to-update-most-recent-opportunity.html
Kiran kumar 193Kiran kumar 193
Previously the same code was working without any issues. Now I am getting this error.

@boris, Getting error in this line "  public void oppupdate(List<opportunity> oplist)"

In trigger Delete contect variable

Please help...