-1
public class contactTriggerHandler {
    Set<Id> AccountIds = new Set<Id>();
    public void performOnAfterInsert(List<Contact> lstCon){

        for (contact con : lstCon ) {        
            if(con.AccountId != null){
                AccountIds.add(con.AccountId);

                List<Account> AcctToUpdate = new List<Account>();
                List<AggregateResult> cont=[Select Count(Id) ContactCount, AccountId from Contact where AccountId IN: AccountIds GROUP BY AccountId];

                for (AggregateResult ar: cont){
                    Account acc = new Account(Id=(Id)ar.get('AccountId'), My_Contact__c=(Integer)ar.get('ContactCount'));
                    AcctToUpdate.add(acc);
                }
                update AcctToUpdate;
            }
        } 
    }


    public void performOnAfterUpdate(List<contact> lstcon) {

        for (contact con : lstcon) {
            if(con.AccountId==null && Trigger.oldMap.get(con.Id).AccountId != null){
                AccountIds.add(Trigger.oldMap.get(con.Id).AccountId);
            }


            if(con.AccountId!=null && Trigger.oldMap.get(con.Id).AccountId != null && con.AccountId != Trigger.oldMap.get(con.Id).AccountId) {
                AccountIds.add(con.AccountId);
                AccountIds.add(Trigger.oldMap.get(con.Id).AccountId);
            }

            if(con.AccountId!=null && Trigger.oldMap.get(con.Id).AccountId == null){
                AccountIds.add(con.AccountId);
            }

            List<Account> AcctToUpdate = new List<Account>();
            List<AggregateResult> cont=[Select Count(Id) ContactCount, AccountId from Contact where AccountId IN: AccountIds GROUP BY AccountId];

            for(AggregateResult ar: cont){
                Account acc = new Account(Id=(Id)ar.get('AccountId'), My_Contact__c=(Integer)ar.get('ContactCount'));
                AcctToUpdate.add(acc);
            }
            update AcctToUpdate;
        } 
    }
}
1
  • 2
    This needs more detail. It's great you included a code sample with an error, but you should add what you've tried, what you're attempting to do, or really just a sentence saying "I hit this error and need some help, does anyone understand the reason for this error?"
    – drakored
    Jan 8, 2016 at 8:53

2 Answers 2

2

Outside of a trigger (e.g. in a handler class like yours) the expression:

Trigger.oldMap.get(con.Id)

evaluates to the generic type SObject which has methods that allow access but does not have the SObject-type specific fields available. The normal solution to this is to pass Trigger.oldMap from the trigger where it evaluates to the correct SObject type (I assume Contact in your case) into the class:

public void performOnAfterUpdate(List<contact> lstcon, Map<Id, Contact> oldMapCon) {
    ...
}

Alternatively you could use the generic methods like get or cast to type Contact.

1

Here problem is in your Trigger.oldMap which is generic for all the object and compiler is not able to identify the object in Trigger.oldMap.

so for that you need to explicitly type cast that at line no. 26 like :

AccountIds.add(((Map<Id, Contact>)Trigger.oldMap).get(con.Id).AccountId);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .