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
CBNCBN 

how to write single SOQL quert to below trigger

Hi All,
Use single SOQL quert to fetch the list of contacts. use if condition to check if contact is active then set active contact accordingly.Put logic in helper class and call tis class from trigger

trigger countContact on Contact (after insert, after update, after delete, after undelete) 
{
  Set<Id> setAccountIds = new Set<Id>();
 
  //Whenever your working with After Undelete operation you can access data through 
  //Trigger.new or Trigger.newMap but not with Trigger.old or Trigger.oldmap variables
  if(Trigger.isInsert || Trigger.isUndelete || Trigger.isUpdate)
  {
   for(Contact con : Trigger.new)
   {
    setAccountIds.add(con.AccountId);
   }
  }
 
  if(Trigger.isDelete)
  {
   //if you use Trigger.new below in place of Trigger.old you will end up with 
   //System.NullPointerException:Attempt to de-reference a null object
   for(Contact con : Trigger.old) 
   {
    setAccountIds.add(con.AccountId);
   }
  }
 
 List<Account> listAccs = [Select id,name,Total_No_Of_Contacts__c ,(Select id from contacts) from Account where Id in : setAccountIds];
  for(Account acc :listAccs)
  {
   acc.Total_No_Of_Contacts__c = acc.contacts.size();
  }
          List<Account> listAccs1 = [Select id,name,No_of_active_contacts__c ,(select id from contacts where Contact_Roll__c = True) from Account where Id in : setAccountIds];
  for(Account acc1 :listAccs1)
  {
   acc1.No_of_active_contacts__c = acc1.contacts.size();
  }
  update listAccs;
  update listAccs1;
 
}

Kindly Support and Suggest
Thanks
Best Answer chosen by CBN
DevADSDevADS
Initialization is missing.
 
public class CountContactHandler
{ 
	public static void countContacts(List<Contact> conList){
		Set<Id> setAccountIds = new Set<Id>();
		for(Contact con : conList)
		{
			setAccountIds.add(con.AccountId);
		}
		
		List<Account> listAccs = [Select id,name,Total_No_Of_Contacts__c ,(Select id,Contact_Roll__c from contacts) from Account where Id in : setAccountIds];
		
		for(Account acc :listAccs)
		{
			Integer i=0,j = 0;
			for(Contact conItr : acc.Contacts){
				if(conItr.Contact_Roll__c)
                { 
                    i++;
                }
				j++;
			
			}
			acc.No_of_active_contacts__c = i;
			acc.Total_No_Of_Contacts__c = j;
		}
		update listAccs;
	}
}

 

All Answers

DevADSDevADS
Hi CBN,

Please see below code - 
public class CountContactHandler
{ 
	public static void countContacts(List<Contact> conList){
		Set<Id> setAccountIds = new Set<Id>();
		for(Contact con : conList)
		{
			setAccountIds.add(con.AccountId);
		}
		
		List<Account> listAccs = [Select id,name,Total_No_Of_Contacts__c ,(Select id,Contact_Roll__c from contacts) from Account where Id in : setAccountIds];
		
		for(Account acc :listAccs)
		{
			Integer i,j = 0;
			for(Contact conItr : acc.Contacts){
				if(conItr.Contact_Roll__c)
                { 
                    i++  
                };
				j++;
			
			}
			acc.No_of_active_contacts__c = i;
			acc.Total_No_Of_Contacts__c = j;
		}
		update listAccs;
	}
}
trigger countContact on Contact (after insert, after update, after delete, after undelete) 
{
	Set<Id> setAccountIds = new Set<Id>();

	//Whenever your working with After Undelete operation you can access data through 
	//Trigger.new or Trigger.newMap but not with Trigger.old or Trigger.oldmap variables
	if(Trigger.isInsert || Trigger.isUndelete || Trigger.isUpdate)
	{
		CountContactHandler.countContacts(Trigger.new);
	}

	else if(Trigger.isDelete)
	{
		//if you use Trigger.new below in place of Trigger.old you will end up with 
		//System.NullPointerException:Attempt to de-reference a null object
		CountContactHandler.countContacts(Trigger.old);
	}
}

Happy Coding!!
 
CBNCBN
Hi DevADS,

I Getting Below error will please find the Solution


Error: Compile Error: Extra ';', at '}'. at line 19 column 17

Thanks,
CBN
CBNCBN
Hi DevADS,

Handler Code is Not working its getting Error While iam testing 


Error: Apex trigger countContact caused an unexpected exception, contact your administrator: countContact: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.CountContactHandler.countContacts: line 18, column 1

Please review once

Thanks 
CBN

 
DevADSDevADS
Initialization is missing.
 
public class CountContactHandler
{ 
	public static void countContacts(List<Contact> conList){
		Set<Id> setAccountIds = new Set<Id>();
		for(Contact con : conList)
		{
			setAccountIds.add(con.AccountId);
		}
		
		List<Account> listAccs = [Select id,name,Total_No_Of_Contacts__c ,(Select id,Contact_Roll__c from contacts) from Account where Id in : setAccountIds];
		
		for(Account acc :listAccs)
		{
			Integer i=0,j = 0;
			for(Contact conItr : acc.Contacts){
				if(conItr.Contact_Roll__c)
                { 
                    i++;
                }
				j++;
			
			}
			acc.No_of_active_contacts__c = i;
			acc.Total_No_Of_Contacts__c = j;
		}
		update listAccs;
	}
}

 
This was selected as the best answer
CBNCBN
Hi DevADS,

Thanks For Your Help 
If you Dint mine Can you give Test Class For this Code Using System.Asserts staements 

Thanks in Advance,