user1568642
user1568642

Reputation: 1

i am not able to resolve the error nullobject reference salesforce trigger

hi i have designed a trigger that provides rollup summary max date value to accounts from its child objects(deals/offers)(they have a lookup relationship with the parent)...but whenever i try to delete a child object i am being thrown an validation error can please help as i am new to salesforce and not able to resolve it.

here is the trigger code:

trigger accountupdate on Deal_Offer__c (after delete, after insert, after update) {
Set<id> accountIds = new Set<id>();
Date maxdate;
Date maxdate1;
Date maxdate2;    
List<Account> accountsToUpdate = new List<Account>();

for (Deal_Offer__c item : Trigger.new)
    accountIds.add(item.Account__c);

if (Trigger.isUpdate || Trigger.isDelete) {
    for (Deal_Offer__c item : Trigger.old)
        accountIds.add(item.Account__c);
}

// get a map of the Accounts and the related fields that need to be updated
Map<id,Account> accountMap = new Map<id,Account>([select id,Most_Recent_Live_Date_Homerun__c,Most_Recent_Live_Date_Chase__c,Most_Recent_Live_Date_Serve__c from Account where id IN :accountIds]);

// query the account and the related deals_offers and update the max_run_date fiels in the account
for (Account acc : [select id,Name,Most_Recent_Live_Date_Homerun__c,Most_Recent_Live_Date_Chase__c,Most_Recent_Live_Date_Serve__c,(select id,Most_Recent_Live_Date_Homerun__c,Most_Recent_Live_Date_Chase__c,Most_Recent_Live_Date_Serve__c from Deals_Offers__r) from Account where id IN :accountIds]) {
   maxdate = acc.Deals_offers__r[0].Most_Recent_Live_Date_Homerun__c;
   maxdate1 = acc.Deals_offers__r[0].Most_Recent_Live_Date_Chase__c;
   maxdate2 = acc.Deals_offers__r[0].Most_Recent_Live_Date_Serve__c;
   for (Integer i = 0;i < acc.Deals_Offers__r.size(); i++){
       if(acc.Deals_Offers__r[i].Most_Recent_Live_Date_Homerun__c > maxdate){
           maxdate = acc.Deals_Offers__r[i].Most_Recent_Live_Date_Homerun__c;
       }
       if(acc.Deals_Offers__r[i].Most_Recent_Live_Date_Chase__c > maxdate){
           maxdate1 = acc.Deals_Offers__r[i].Most_Recent_Live_Date_Chase__c;
       }
       if(acc.Deals_Offers__r[i].Most_Recent_Live_Date_Serve__c > maxdate){
           maxdate2 = acc.Deals_Offers__r[i].Most_Recent_Live_Date_Serve__c;
       }                                     
   }
  accountMap.get(acc.Id).Most_Recent_Live_Date_Homerun__c = maxdate;
  accountMap.get(acc.Id).Most_Recent_Live_Date_Chase__c = maxdate1;
  accountMap.get(acc.Id).Most_Recent_Live_Date_Serve__c = maxdate2;
  //add the account to update list          
  accountsToUpdate.add(accountMap.get(acc.Id));  

}

update accountsToUpdate;

}

this is the error i am getting when i delete an deal/offer from the account

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger accountupdate caused an unexpected exception, contact your administrator: accountupdate: execution of AfterDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.accountupdate: line 8, column 1".

can please tell me where i am getting it wrong and what should it do to remedy it.

thanks in advance

Upvotes: 0

Views: 2367

Answers (1)

Ajay Dubedi
Ajay Dubedi

Reputation: 208

Trigger.New and Trigger.old both are trigger context variables. New gives you the updated values where as Old gives you the prior values of the record.

Trigger.new is available in Before Insert, After Insert, Before Update, After Update

Trigger.old is available in Before Update, After Update, Before Delete, After Delete.

Your code is trying to use Trigger.New in after delete where the Record is deleted and its Obvious that after deletion Trigger.New will have null.

Please Try to use the
if (Trigger.isDelete) In your code to make the blocks of code being executed for a specific Operation(Insert, delete, update). Click here to view all the Apex Trigger Context variable.

Upvotes: 2

Related Questions