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
影響を受けるレコードの保存中にカスタム検証エラーが発生しました。最初に発生した検証エラーは、「Apex トリガ accountupdate により予期しない例外が発生しました。管理者に連絡してください: accountupdate: AfterDelete の実行が原因: System.NullPointerException: Attempt to de-reference a null object: Trigger.accountupdate: line 8, column 1」でした。 .
どこが間違っているのか、それを修正するにはどうすればよいのか教えてください。
前もって感謝します