• Satvik Shukla
  • NEWBIE
  • 0 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

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); 
    }
}

}

 

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); 
    }
}

}