0

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

4

1 回答 1

2

Trigger.NewTrigger.old都是触发器上下文变量。New 为您提供更新的值,而 Old 为您提供记录的先前值。

Trigger.new在插入前、插入后、更新前、更新后可用

Trigger.old可用于更新前、更新后、删除前、删除后。

您的代码尝试在删除记录被删除的位置后使用 Trigger.New,并且很明显,删除后 Trigger.New 将具有空值。

请尝试
在您的代码中使用 if (Trigger.isDelete) 来为特定操作(插入、删除、更新)执行代码块。 单击此处查看所有 Apex 触发器上下文变量。

于 2012-09-26T09:16:22.063 回答