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
Satvik ShuklaSatvik Shukla 

after delete trigger not working on the last record

Hi, 

I have created a trigger which is on account team member object, this trigger is initiated when we delete(after delete) an account team member that is associated with an account. My trigger is working in all the conditions expect for the last record. ex: when there is only one member left in the account team member it is able to delete it on the account team member object but, the same thing is not been reflected on the Account object's network id field. On this field Account.network_id__c field I am trying to log all the account team members associated with that account. 

Below is the trigger  along with the helper class

trigger AccountTeamMemberTrigger on AccountTeamMember (after delete, after update) {
    
    if(trigger.isAfter){
         if(trigger.isDelete){
        AccountTeamMemberTriggerHelper.updateAccountNetworkId(trigger.old);
    }}
    

 

-------------------helper class------------
public without sharing class AccountTeamMemberTriggerHelper {
    private static Set<Id> accountIdSet= new Set<Id>();
    private static List<Account> accListToUpdate = new List<Account>();
    private static Map<Id, String> accountMapInfo = new Map<Id, String>();
    
public static void updateAccountNetworkId(List<AccountTeamMember> triggerNew){
    for(AccountTeamMember atm : triggernew){
       accountIdSet.add(atm.AccountId);
    }

    networkIdProcess();
}

private static void networkIdProcess(){
    for(AccountTeamMember atm : [SELECT User.Network_Id__c, AccountId FROM AccountTeamMember WHERE AccountId IN : accountIdSet]){
        addNetworkId(atm);
    }

    // Loop through the account map a second time to update the accounts
    for(Id accId : accountMapInfo.keySet()){
        Account acc = new Account(Id = accId, Account_Team_Network_ID_String__c = accountMapInfo.get(accId));
        accListToUpdate.add(acc);
    }

    if(accListToUpdate.size() > 0) updateAccounts();
}

private static void addNetworkId(AccountTeamMember atm){
    String teamNetworkId = accountMapInfo.get(atm.AccountId) != null ? accountMapInfo.get(atm.AccountId) : '';

   if(String.isNotBlank(atm.User.Network_Id__c))
        {
            teamNetworkId += atm.User.Network_Id__c + '-';
        }

    accountMapInfo.put(atm.AccountId, teamNetworkId);
}

private static void updateAccounts(){
    Savepoint sp = Database.setSavepoint();
    try{
        update accListToUpdate;
    } catch(DmlException e){
        system.debug('Error updating the Account: ' + e);
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Could not update the Account on Trigger. Please contact your system administrator.'));
        Database.rollback(sp); 
    }
}

}

 

SubratSubrat (Salesforce Developers) 
Hello ,

Based on your trigger and helper class, it seems that the issue lies in the logic of your networkIdProcess method. The method is not correctly handling the scenario when there is only one member left in the account team.

To resolve this issue, you need to modify the networkIdProcess method as follows:
private static void networkIdProcess() {
    for (AccountTeamMember atm : [SELECT User.Network_Id__c, AccountId FROM AccountTeamMember WHERE AccountId IN :accountIdSet]) {
        addNetworkId(atm);
    }

    // Loop through the account map to update the accounts and remove the last network ID if necessary
    for (Id accId : accountMapInfo.keySet()) {
        Account acc = new Account(Id = accId, Account_Team_Network_ID_String__c = accountMapInfo.get(accId));
        if (accountMapInfo.get(accId).endsWith('-')) {
            // Remove the trailing '-' if it exists
            acc.Account_Team_Network_ID_String_c = acc.Account_Team_Network_ID_String_c.removeEnd('-');
        }
        accListToUpdate.add(acc);
    }

    if (accListToUpdate.size() > 0) {
        updateAccounts();
    }
}
The modified code checks if the Account_Team_Network_ID_String__c ends with a '-' character. If it does, it means that there is only one member left in the account team. In such cases, it removes the trailing '-' to ensure that the network ID string is correct.

Additionally, ensure that you have appropriately defined the Account trigger and included the necessary logic for other trigger events like after update. The provided code only covers the after delete trigger event.

Make sure to test the updated trigger and helper class to ensure that the Account's network_id__c field is correctly updated when deleting the last member from the AccountTeamMember object.

Hope this helps !
Thank you.
Satvik ShuklaSatvik Shukla

Hi, Subrat 

This solution is not working. 

 

Satvik

Andrew Gillan 30Andrew Gillan 30
I would suspect the issue is that it is an AFTER delete trigger which you are then using to try and get your ATM (AccountTeamMember) records.

basically, the ATM record is deleted, the code run AFTER that delete, your query where you get the remaining ATMs for the Account associated with the last deleted record has ZERO atms associated with it - they are deleted.

So you need to test the return set from your query of ATMs and see if it has values.  If no values, adjust the code so that your field in the account record is updated accordingly.
 
for(AccountTeamMember atm : [SELECT User.Network_Id__c, AccountId FROM AccountTeamMember WHERE AccountId IN : accountIdSet]){
        addNetworkId(atm);
    }

if (addNetworkId.size() = 0 ) {
    // clear the team values in the account record
    account.Account_Team_Network_ID_String__c = account.Id;
} else 
{
    networkIdProcess();
}

above is provided as psuedo code and is not guaranteed to work - but you should get the idea

regards

Andrew