+ Start a Discussion
Nichole SmithNichole Smith 

Task Trigger Issue when updating Rolling Up Related Records

I have created a Task Trigger to count the number of Calls related to an Account from all of the related record (Contacts, Opportunities) and used 2 fields on the Account (Activity Count, AS Activity Count) to store the count in depending on the Rep who was making the call. The issue I am having though is that the Account part is working great but the Contact and Opportunity is recieving an error stating that the Account Id was recieved without querying the requested field. Listed below is my trigger. What am I doing wrong?
trigger Activity_Count on Task (before insert, before update){

    Set<Id> accIds = new Set<Id>();
   Map<Id,Account> accMap = new Map<Id,Account>();
    Map<Id,Task> conMap = new Map<Id,Task>();
    Map<Id,Task> oppMap = new Map<Id, Task>();
     List<Account> updateAccountList = new List<Account>();  
   List<Contact> udpateContactList = new List<Contact>();
    List<Opportunity> updateOppList = new List<Opportunity>();
    String accPrefix = Account.sObjectType.getDescribe().getKeyPrefix();
    String contactPrefix = Contact.sObjectType.getDescribe().getKeyPrefix();
    String oppPrefix= Opportunity.sObjectType.getDescribe().getKeyPrefix();
 
        
    for (Task t :  Trigger.new) {
     
     String str = t.whatId;
       String contactstr = t.WhoId;
        
          if(t.WhoId!=null && t.Subject.contains('Call') && contactstr.startsWith(contactPrefix) && t.status =='Completed'  && t.Counted__c != True)      //Task belongs to the Contact
          {
                        conMap.put(t.whoId, t);
              			   t.Counted__c=true;
            }
        if(t.WhatId!=null && t.Subject.contains('Call') && str.startsWith(accPrefix) && t.status =='Completed' && t.Counted__c !=True)     // Task belongs to Account with AS Rep Owner
          {
            accIds.add(t.whatId);
           t.Counted__c=true;
          }
         if(t.WhatId != null && t.Subject.contains('Call') && str.startsWith(oppPrefix) && t.status =='Completed' && t.Counted__c != True)     //Task belongs to Opportunity
        {
            
                  oppMap.put(t.whatId, t);
            		   t.Counted__c=true;
   		 } 
    
    }
    try{
String ProfileId = userinfo.getProfileId();
if(accIds.size() > 0){ 
              accMap = new Map<Id, Account> ([SELECT Id, 
                                              Activity_Count__c,
                                              AS_Activity_Count__c
                                           FROM Account WHERE Id in :accIds]);
           updateAccountList = accMap.values();
         
               for(Account acc : updateAccountList){
                     if(acc.AS_Activity_Count__c != null){
                           acc.AS_Activity_Count__c =  acc.AS_Activity_Count__c +1;
                        }
                   If(ProfileId =='00e30000001wVmqAAE'){
                          acc.AS_Activity_Count__c =1; 
                     }
                   if(acc.Activity_Count__c != null && ProfileId =='00e30000001wWXJAA2'){
                        acc.Activity_Count__c =  acc.Activity_Count__c +1;
                   }
                  If(ProfileId=='00e30000001wWXJ'){
                         acc.Activity_Count__c =1;
                   }
               }
         update updateAccountList;
        }
If(conMap.size()>0){ //Updating Accounts when Task is logged on Contact
                
          udpateContactList = [Select Id, AccountId from Contact where Id IN: conMap.keySet()];
          
          for(Contact con : udpateContactList){
				  If(con.Account.AS_Activity_Count__c!=null && ProfileId =='00e30000001wVmq'){
                          con.Account.AS_Activity_Count__c = con.Account.AS_Activity_Count__c+1; 
                     }
                If(ProfileId =='00e30000001wVmq'){
                         con.Account.AS_Activity_Count__c =1; 
                     }
                   if(con.Account.Activity_Count__c != null && ProfileId =='00e30000001wWXJ'){
                        con.Account.Activity_Count__c = con.Account.Activity_Count__c +1;
                   }
                  If(ProfileId=='00e30000001wWXJ'){
                         con.Account.Activity_Count__c =1;
                   }
             }
         update udpateContactList;
        }
      
    If(oppMap.size() >0){ //Updating Accounts when call is logged on Opportunity
      updateOppList = [Select Id, AccountId from Opportunity where Id IN: oppMap.keySet()];
         for(Opportunity opps : updateOppList){  
                   If(opps.Account.AS_Activity_Count__c!=null && ProfileId =='00e30000001wVmq'){
                         opps.Account.AS_Activity_Count__c =  opps.Account.AS_Activity_Count__c +1; 
                     }
                 If(ProfileId =='00e30000001wVmq'){
                         opps.Account.AS_Activity_Count__c =1; 
                     }
                   if( ProfileId =='00e30000001wWXJ' && opps.Account.Activity_Count__c != null ){
                        opps.Account.Activity_Count__c =  opps.Account.Activity_Count__c +1;
                   }
                  If(ProfileId=='00e30000001wWXJ'){
                         opps.Account.Activity_Count__c =1;
                   }
             }
         update updateOppList;
    
             }
    }
  catch(Exception e){
        System.debug('Exception in Trigger:Activity_Count'  + e.getMessage());
    }
    
}
Best Answer chosen by Nichole Smith
Asif Ali MAsif Ali M
The reason for the Error is you are trying to update parent fields which are not retrived in your contact/opportunity SOQL . Please find the below code to fix the issue. My changes are manyly in the conMap/oppMap if block. Also commented the update statement in first if block and added it at the end of try block. Let me know if you get any issues.
 
try {
    String ProfileId = userinfo.getProfileId();
    if (accIds.size() > 0) {
        accMap = new Map<Id, Account>([
                SELECT Id,
                        Activity_Count__c,
                        AS_Activity_Count__c
                FROM Account
                WHERE Id in :accIds
        ]);
        updateAccountList = accMap.values();

        for (Account acc : updateAccountList) {
            if (acc.AS_Activity_Count__c != null) {
                acc.AS_Activity_Count__c = acc.AS_Activity_Count__c + 1;
            }
            If (ProfileId == '00e30000001wVmqAAE') {
                acc.AS_Activity_Count__c = 1;
            }
            if (acc.Activity_Count__c != null && ProfileId == '00e30000001wWXJAA2') {
                acc.Activity_Count__c = acc.Activity_Count__c + 1;
            }
            If (ProfileId == '00e30000001wWXJ') {
                acc.Activity_Count__c = 1;
            }
        }
        // COMMENTED THIS LINE
        //update updateAccountList; 
    }
    If (conMap.size() > 0) { //Updating Accounts when Task is logged on Contact


        Account[] contactAccounts = [SELECT Id, Activity_Count__c, AS_Activity_Count__c FROM Account where ID in (Select AccountId from Contact where Id IN:conMap.keySet())];

        for (Account acct : contactAccounts) {
            If (acct.AS_Activity_Count__c != null && ProfileId == '00e30000001wVmq') {
                acct.AS_Activity_Count__c = acct.AS_Activity_Count__c + 1;
            }
            If (ProfileId == '00e30000001wVmq') {
                acctAS_Activity_Count__c = 1;
            }
            if (acct.Activity_Count__c != null && ProfileId == '00e30000001wWXJ') {
                acct.Activity_Count__c = acct.Activity_Count__c + 1;
            }
            If (ProfileId == '00e30000001wWXJ') {
                acct.Activity_Count__c = 1;
            }
        }
        // Add contactAccounts to the list
        updateAccountList.addAll(contactAccounts);
    }

    If (oppMap.size() > 0) { //Updating Accounts when call is logged on Opportunity
        Account[] oppAccounts = [SELECT Id, Activity_Count__c, AS_Activity_Count__c FROM Account where Id In (Select AccountId from Opportunity where Id IN:oppMap.keySet()];
        for (Account oppAct: oppAccounts) {
            If (oppActAS_Activity_Count__c != null && ProfileId == '00e30000001wVmq') {
                oppAct.AS_Activity_Count__c = oppAct.AS_Activity_Count__c + 1;
            }
            If (ProfileId == '00e30000001wVmq') {
                oppAct.AS_Activity_Count__c = 1;
            }
            if (ProfileId == '00e30000001wWXJ' && oppAct.Activity_Count__c != null) {
                oppAct.Activity_Count__c = oppAct.Activity_Count__c + 1;
            }
            If (ProfileId == '00e30000001wWXJ') {
                oppAct.Activity_Count__c = 1;
            }
        }

        // Add oppotunity Account to the list
        updateAccountList.addAll(oppAccounts);

    }


    // Update all Accounts in one go
    update updateAccountList;
} catch (Exception e) {
    System.debug('Exception in Trigger:Activity_Count' + e.getMessage());
}

 

All Answers

Asif Ali MAsif Ali M
The reason for the Error is you are trying to update parent fields which are not retrived in your contact/opportunity SOQL . Please find the below code to fix the issue. My changes are manyly in the conMap/oppMap if block. Also commented the update statement in first if block and added it at the end of try block. Let me know if you get any issues.
 
try {
    String ProfileId = userinfo.getProfileId();
    if (accIds.size() > 0) {
        accMap = new Map<Id, Account>([
                SELECT Id,
                        Activity_Count__c,
                        AS_Activity_Count__c
                FROM Account
                WHERE Id in :accIds
        ]);
        updateAccountList = accMap.values();

        for (Account acc : updateAccountList) {
            if (acc.AS_Activity_Count__c != null) {
                acc.AS_Activity_Count__c = acc.AS_Activity_Count__c + 1;
            }
            If (ProfileId == '00e30000001wVmqAAE') {
                acc.AS_Activity_Count__c = 1;
            }
            if (acc.Activity_Count__c != null && ProfileId == '00e30000001wWXJAA2') {
                acc.Activity_Count__c = acc.Activity_Count__c + 1;
            }
            If (ProfileId == '00e30000001wWXJ') {
                acc.Activity_Count__c = 1;
            }
        }
        // COMMENTED THIS LINE
        //update updateAccountList; 
    }
    If (conMap.size() > 0) { //Updating Accounts when Task is logged on Contact


        Account[] contactAccounts = [SELECT Id, Activity_Count__c, AS_Activity_Count__c FROM Account where ID in (Select AccountId from Contact where Id IN:conMap.keySet())];

        for (Account acct : contactAccounts) {
            If (acct.AS_Activity_Count__c != null && ProfileId == '00e30000001wVmq') {
                acct.AS_Activity_Count__c = acct.AS_Activity_Count__c + 1;
            }
            If (ProfileId == '00e30000001wVmq') {
                acctAS_Activity_Count__c = 1;
            }
            if (acct.Activity_Count__c != null && ProfileId == '00e30000001wWXJ') {
                acct.Activity_Count__c = acct.Activity_Count__c + 1;
            }
            If (ProfileId == '00e30000001wWXJ') {
                acct.Activity_Count__c = 1;
            }
        }
        // Add contactAccounts to the list
        updateAccountList.addAll(contactAccounts);
    }

    If (oppMap.size() > 0) { //Updating Accounts when call is logged on Opportunity
        Account[] oppAccounts = [SELECT Id, Activity_Count__c, AS_Activity_Count__c FROM Account where Id In (Select AccountId from Opportunity where Id IN:oppMap.keySet()];
        for (Account oppAct: oppAccounts) {
            If (oppActAS_Activity_Count__c != null && ProfileId == '00e30000001wVmq') {
                oppAct.AS_Activity_Count__c = oppAct.AS_Activity_Count__c + 1;
            }
            If (ProfileId == '00e30000001wVmq') {
                oppAct.AS_Activity_Count__c = 1;
            }
            if (ProfileId == '00e30000001wWXJ' && oppAct.Activity_Count__c != null) {
                oppAct.Activity_Count__c = oppAct.Activity_Count__c + 1;
            }
            If (ProfileId == '00e30000001wWXJ') {
                oppAct.Activity_Count__c = 1;
            }
        }

        // Add oppotunity Account to the list
        updateAccountList.addAll(oppAccounts);

    }


    // Update all Accounts in one go
    update updateAccountList;
} catch (Exception e) {
    System.debug('Exception in Trigger:Activity_Count' + e.getMessage());
}

 
This was selected as the best answer
Asif Ali MAsif Ali M
There is still scope to refactor all 3 if blocks into one and make one single SOQL to avoid hitting system limits.
Nichole SmithNichole Smith
Thank you Asif. That makes sense.