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
nitin sharmanitin sharma 

Trigger on contact object and showing results on the account object

Hi,

I have checkbox field on the contact object and i have to show the  count of the contacts on which this checkbox field have been checked on the account object..I am getting the followign error.I am not sure where I am going Wrong.Can somebody please help?

The error is:-Method does not exist or incorrect signature: [String].Size() at line 32 column 39


trigger BillingContactsCount on Contact (after Insert,after update) {
list<id> accountids=new list<id>();
for(contact c:trigger.new)
    {
        if(c.billingcontacts__c==true)
            {
         
           
            Accountids.add(c.accountid);
            System.debug('The account type checks are as follows'+Accountids.get(0));
           

            }
      
                 
    }

    list<contact> cc=[Select id,billingcontacts__c,Accountid from contact where accountid in:Accountids and billingcontacts__c=true];
    Map<ID,String>m=new map<ID,String>();
  
    for(Contact d:cc)
    {
   
     m.put(d.accountid,d.id);
    
   
    }
    for(Id ids:Accountids)
    {
   
    Account acc=new account(id=ids);
    acc.ContactCount__c=m.get(acc.id).Size();
   
    }
  }
Best Answer chosen by nitin sharma
Pavan Kumar KajaPavan Kumar Kaja
Hi,

Try below code.


trigger BillingContactsCount on Contact (after Insert,after update) {
	Set<Id> setAccountids=new Set<Id>();
	list<Contact> lstAccountToUpdate = new list<Contact>();
	Account acc;
	
	for(Contact c:trigger.new){
        if(c.billingcontacts__c==true){
            setAccountids.add(c.AccountId);
		}            
    }
	
	if(!setAccountids.isEmpty()){
		List<AggregateResult> lstAggr = [SELECT COUNT(Id) count,AccountId  FROM Contact WHERE AccountId IN :setAccountids GROUP By AccountId];

		for(AggregateResult ag : lstAggr){
			string accId = (string) ag.get('AccountId');
			integer count = (integer) ag.get('count');
			
			acc = new Account(id = accId, ContactCount__c=  count);
			lstAccountToUpdate.add(acc);
		}
		
		try{
			lstAccountToUpdate;
		}
		catch(DMLException e){
			for(Contact c: Trigger.new){
				c.addError(e.getDmlMessage(0));
			}
		}
	}
}


All Answers

Pavan Kumar KajaPavan Kumar Kaja
Hi,

Try below code.


trigger BillingContactsCount on Contact (after Insert,after update) {
	Set<Id> setAccountids=new Set<Id>();
	list<Contact> lstAccountToUpdate = new list<Contact>();
	Account acc;
	
	for(Contact c:trigger.new){
        if(c.billingcontacts__c==true){
            setAccountids.add(c.AccountId);
		}            
    }
	
	if(!setAccountids.isEmpty()){
		List<AggregateResult> lstAggr = [SELECT COUNT(Id) count,AccountId  FROM Contact WHERE AccountId IN :setAccountids GROUP By AccountId];

		for(AggregateResult ag : lstAggr){
			string accId = (string) ag.get('AccountId');
			integer count = (integer) ag.get('count');
			
			acc = new Account(id = accId, ContactCount__c=  count);
			lstAccountToUpdate.add(acc);
		}
		
		try{
			lstAccountToUpdate;
		}
		catch(DMLException e){
			for(Contact c: Trigger.new){
				c.addError(e.getDmlMessage(0));
			}
		}
	}
}


This was selected as the best answer
nitin sharmanitin sharma
Thanks Ashi,

You have been of graeat help