You need to sign in to do that
Don't have an account?
Nichole 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()); } }
All Answers