function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Adam MarksAdam 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:
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.
Best Answer chosen by Adam Marks
Marty C.Marty C.
Hello, Adam, I think the solution may be as easy as removing the after delete keywords in the trigger definition. This code appears to be designed to set up account teams. Since account teams become irrelevant when you delete an account, there's no need for this trigger to do anything on delete operations.

All Answers

Jeff MayJeff May
You can't use trigger.new in After Delete triggers -- you have to use trigger.old.

You can do this:

List<Account> theaccounts = new List<Account>();

if (trigger.isDelete){
    theaccounts = trigger.old;
} else {
   theaccounts = trigger.new;
}
Jeff MayJeff May
use the above if test to set theaccounts list, then use that in your for loop.

instead of:

for (Account a : trigger.new)

use 

for (Account a : theaccounts)

Marty C.Marty C.
Hello, Adam, I think the solution may be as easy as removing the after delete keywords in the trigger definition. This code appears to be designed to set up account teams. Since account teams become irrelevant when you delete an account, there's no need for this trigger to do anything on delete operations.
This was selected as the best answer
Jeff MayJeff May
Oh, and you'll also have to use this in the WHERE clause of your Searchable_Account__c query.
Jeff MayJeff May
Good point, Marty!!!  
Adam MarksAdam Marks
Thanks guys. Marty's suggestion worked so the records are deleting properly now. I am wondering if the the Trigger was supposed to mash up Account Team members when a record is merged. I think that was the goal for the after delete part. Unfortunatly the client is not sure and I don't have a way to contact the group that wrote the trigger initially.