You need to sign in to do that
Don't have an account?
Adam Marks
Trigger Error
Need some help with a Trigger Error. I am not a developer so my skills get stretched with things like this. Also, for whatever it's worth I did not write the Trigger but am in a position to support it. The user is getting the following error when they try and merge records: "AccountTrigger: execution of AfterDelete caused by: System.NullPointerException: Attempt to de-reference a null object: External entry point"."
See below Trigger syntax:
See below Trigger syntax:
trigger AccountTrigger on Account (before insert, after insert, after update, after delete) { String PROSPECT_RT_ID = dao_Account.getInstance().getRecordTypeId('Prospect'); User defaultOwner = dao_User.getInstance().getUserByName('Medical-Surgical IT Data'); List<AccountTeamMember> atmInserts = new List<AccountTeamMember>(); List<AccountShare> ashInserts = new List<AccountShare>(); String LTC_REP = 'EC Long Term Care Rep'; String HC_REP = 'EC Home Care DME Rep'; Map<Id, Searchable_Account__c> saMap = new Map<Id, Searchable_Account__c>(); for (Searchable_Account__c sa : [Select Id, Account__c, Account_Name_ExtId__c from Searchable_Account__c where Account__c in :trigger.new]){ saMap.put(sa.Account__c, sa); } for (Account a : Trigger.new) { if (a.RecordTypeId == PROSPECT_RT_ID) { if (trigger.isBefore) { a.OwnerId = defaultOwner.Id; } else if (!trigger.isDelete){ User originalOwner = dao_User.getInstance().getUser(a.CreatedById); AccountTeamMember atm = new AccountTeamMember(); AccountShare ash = new AccountShare(); atm.AccountId = a.Id; atm.UserId = originalOwner.Id; if ( originalOwner.Profile.Name.contains('HomeCare') || (originalOwner.UserRoleId != null && originalOwner.UserRole.Name.startsWith('HC')) ) { atm.TeamMemberRole = HC_REP; } else if ( originalOwner.Profile.Name.contains('LongTermCare') || (originalOwner.UserRoleId != null && originalOwner.UserRole.Name.startsWith('LTC')) ) { atm.TeamMemberRole = LTC_REP; } atmInserts.add(atm); ash.AccountAccessLevel = 'Edit'; ash.AccountId = a.Id; ash.OpportunityAccessLevel = 'None'; ash.UserOrGroupId = originalOwner.Id; ashInserts.add(ash); } } else if (trigger.isAfter) { Searchable_Account__c sa = saMap.get(a.id); if (sa== null) sa = new Searchable_Account__c(); sa.Account__c = a.Id; sa.Account_Name_ExtId__c = a.Name + ' | ' + a.AccountNumber; saMap.put(a.Id,sa); } } insert atmInserts; insert ashInserts; upsert saMap.values(); }I understand what is happening, and to an extent why but I am at a loss as to how to fix it. Thanks in advance for any guidance.
All Answers
You can do this:
List<Account> theaccounts = new List<Account>();
if (trigger.isDelete){
theaccounts = trigger.old;
} else {
theaccounts = trigger.new;
}
instead of:
for (Account a : trigger.new)
use
for (Account a : theaccounts)