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
Ravindar AdminRavindar Admin 

Incompatible key type Account for Map<Id,Integer> at line 7 column 21

trigger CountCont  on contact (after insert, after delete) {
    map<Id, Integer> mapcount = new map<Id, Integer>();
    if(trigger.isInsert) {
        for(contact c : trigger.new) {
            if(c.account != null) {
                if(!mapcount.containsKey(c.account)) {
                    mapcount.put(c.account, 1);
                } else {
                    mapcount.put(c.account, mapcount.get(c.account) + 1);
                }
            }
        }
    } else {
        for(contact c : trigger.old) {
            if(c.account != null) {
                if(!mapcount.containsKey(c.account)) {
                    mapcount.put(c.account, -1);
                } else {
                    mapcount.put(c.account, mapcount.get(c.account) - 1);
                }
            }
        }
    }
    if(mapcount.size() > 0) {
        List<account> a = [SELECT Id, size_of_account__c FROM account WHERE Id IN : mapcount.keySet()];
        
        for(account ac : a) {
            a.size_of_account__c += mapcount.get(a.Id);
        }
        
        update a;
    }
}
 
AkhilMandiaAkhilMandia
Hi,

please use this at line 7
mapcount.put(c.accountID, 1);


 
Amit Chaudhary 8Amit Chaudhary 8
updated your code like below
trigger CountCont  on contact (after insert, after delete) 
{
    map<Id, Integer> mapcount = new map<Id, Integer>();
    if(trigger.isInsert) 
	{
        for(contact c : trigger.new) 
		{
            if(c.accountId != null) 
			{
                if(!mapcount.containsKey(c.accountId)) 
				{
                    mapcount.put(c.accountId, 1);
                } else {
                    mapcount.put(c.accountId, mapcount.get(c.accountId) + 1);
                }
            }
        }
    } 
	else 
	{
        for(contact c : trigger.old) 
		{
            if(c.accountId != null) 
			{
                if(!mapcount.containsKey(c.accountId)) 
				{
                    mapcount.put(c.accountId, -1);
                } 
				else 
				{
                    mapcount.put(c.accountId, mapcount.get(c.accountId) - 1);
                }
            }
        }
    }
	
    if(mapcount.size() > 0) {
        List<account> a = [SELECT Id, size_of_account__c FROM account WHERE Id IN : mapcount.keySet()];
        
        for(account ac : a) {
            a.size_of_account__c += mapcount.get(a.Id);
        }
        
        update a;
    }
}

Let us know if this will help you