+ Start a Discussion
SFDC Lightning 18SFDC Lightning 18 

Trigger help on Bulkify

Hi friends, I need hlep on this trigger to bulkify, this trigger is rollupsummary fields to update in account, can you guys please suggest to change the trigger in best way, actually trigger is working fiine in sandbox, but in production some times cutomer getting error like this -- CountCheckbox : CountCheckbox: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object ().

 
Trigger CountCheckbox on Contact (after delete, after insert, after undelete,
after update) {

    Contact[] cons;
    if (Trigger.isDelete)
        cons = Trigger.old;
    else
        cons = Trigger.new;

    // get list of accounts
    Set<ID> acctIds = new Set<ID>();
    for (Contact con : cons) {
            acctIds.add(con.AccountId);
    }
    
    List<account> accountsToUpdate=new list<Account>();
   
   
    Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([select Id
                                                            ,AccountId,Is_Promoter__c,Is_Detractor__c,Is_Passive__c,Is_Promoter_ETS__c,Is_Detractor_ETS__c,Is_Passive_ETS__c,CES_Value__c,CES_Count__c
                                                            from Contact
                                                            where AccountId in :acctIds]);

    Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id
                                                                 ,RMT_Promoters_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);
    Map<ID, Account> acctsToUpdate2 = new Map<ID, Account>([select Id
                                                                 ,RMT_Detractors_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);    
    Map<ID, Account> acctsToUpdate3 = new Map<ID, Account>([select Id
                                                                 ,RMT_Passive_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);   
    Map<ID, Account> acctsToUpdate4 = new Map<ID, Account>([select Id
                                                                 ,ETS_Promoters_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);
    Map<ID, Account> acctsToUpdate5 = new Map<ID, Account>([select Id
                                                                 ,ETS_Detractors_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);    
    Map<ID, Account> acctsToUpdate6 = new Map<ID, Account>([select Id
                                                                 ,ETS_Passive_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]); 
    Map<ID, Account> acctsToUpdate7 = new Map<ID, Account>([select Id
                                                                 ,No_of_CES_response__c
                                                                  from Account
                                                                  where Id in :acctIds]);     
    Map<ID, Account> acctsToUpdate8 = new Map<ID, Account>([select Id
                                                                 ,Total_sum_of_CES_response__c
                                                                  from Account
                                                                  where Id in :acctIds]); 

    for (Account acct : acctsToUpdate.values()) {
        Set<ID> conIds = new Set<ID>();
        for (Contact con : contactsForAccounts.values()) {
            if (con.AccountId == acct.Id && con.Is_Promoter__c == TRUE)
                conIds.add(con.Id);
        }
        if (acct.RMT_Promoters_Count__c != conIds.size())
            acct.RMT_Promoters_Count__c = conIds.size();
    }

    update acctsToUpdate.values();
    
    for (Account acct2 : acctsToUpdate2.values()) {
        Set<ID> conIds2 = new Set<ID>();
        for (Contact con2 : contactsForAccounts.values()) {
            if (con2.AccountId == acct2.Id && con2.Is_Detractor__c == TRUE)
                conIds2.add(con2.Id);
        }
        if (acct2.RMT_Detractors_Count__c != conIds2.size())
            acct2.RMT_Detractors_Count__c = conIds2.size();
    }

    update acctsToUpdate2.values();
    
    for (Account acct3 : acctsToUpdate3.values()) {
        Set<ID> conIds3 = new Set<ID>();
        for (Contact con3 : contactsForAccounts.values()) {
            if (con3.AccountId == acct3.Id && con3.Is_Passive__c == TRUE)
                conIds3.add(con3.Id);
        }
        if (acct3.RMT_Passive_Count__c != conIds3.size())
            acct3.RMT_Passive_Count__c = conIds3.size();
    }

    update acctsToUpdate3.values();
    

    
    for (Account acct4 : acctsToUpdate4.values()) {
        Set<ID> conIds4 = new Set<ID>();
        for (Contact con4 : contactsForAccounts.values()) {
            if (con4.AccountId == acct4.Id && con4.Is_Promoter_ETS__c == TRUE)
                conIds4.add(con4.Id);
        }
        if (acct4.ETS_Promoters_Count__c != conIds4.size())
            acct4.ETS_Promoters_Count__c = conIds4.size();
    }

    update acctsToUpdate4.values();
    
    for (Account acct5 : acctsToUpdate5.values()) {
        Set<ID> conIds5 = new Set<ID>();
        for (Contact con5 : contactsForAccounts.values()) {
            if (con5.AccountId == acct5.Id && con5.Is_Detractor_ETS__c == TRUE)
                conIds5.add(con5.Id);
        }
        if (acct5.ETS_Detractors_Count__c != conIds5.size())
            acct5.ETS_Detractors_Count__c = conIds5.size();
    }

    update acctsToUpdate5.values();
    
    for (Account acct6 : acctsToUpdate6.values()) {
        Set<ID> conIds6 = new Set<ID>();
        for (Contact con6 : contactsForAccounts.values()) {
            if (con6.AccountId == acct6.Id && con6.Is_Passive_ETS__c == TRUE)
                conIds6.add(con6.Id);
        }
        if (acct6.ETS_Passive_Count__c != conIds6.size())
            acct6.ETS_Passive_Count__c = conIds6.size();
    }

    update acctsToUpdate6.values();
    
    for (Account acct7 : acctsToUpdate7.values()) {
        Set<ID> conIds7 = new Set<ID>();
        for (Contact con7 : contactsForAccounts.values()) {
            if (con7.AccountId == acct7.Id && con7.CES_Count__c != 0 )
                conIds7.add(con7.Id);
        }
        if (acct7.No_of_CES_response__c != conIds7.size())
            acct7.No_of_CES_response__c = conIds7.size();
    }
    
    update acctsToUpdate7.values();
    
    
     Map<Id, Account> acc= new Map<Id, Account>([Select Id, Total_sum_of_CES_response__c From Account Where Id In :acctIds]);
    AggregateResult[] groupedResults = [SELECT AccountId,SUM(CES_Value__c)amt FROM contact where AccountId in:acctIds group by AccountId];

      for (AggregateResult ar : groupedResults ) {
        acc.get(String.valueOf(ar.get('Accountid'))).Total_sum_of_CES_response__c = Integer.valueOf(ar.get('amt'));
        accountsToUpdate.add(acc.get(String.valueOf(ar.get('Accountid'))));
    }
    update accountsToUpdate;    
    
}

 
Danish HodaDanish Hoda
Hi,
Have null checks before every DML/for-loop.
e.g. befor line #057
if(!acctsToUpdate.isEmpty()){
for (Account acct : acctsToUpdate.values()) {....}
}
SFDC Lightning 18SFDC Lightning 18
Can you please check below code is best practice to write trigger.if not can please help me on this.Thanks

 
trigger CountCheckbox on Contact (after delete, after insert, after undelete,
after update) {

    Contact[] cons;
    if (Trigger.isDelete)
        cons = Trigger.old;
    else
        cons = Trigger.new;

    // get list of accounts
    Set<ID> acctIds = new Set<ID>();
    for (Contact con : cons) {
            acctIds.add(con.AccountId);
    }
    
    List<account> accountsToUpdate=new list<Account>();
   
   
    Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([select Id
                                                            ,AccountId,Is_Promoter__c,Is_Detractor__c,Is_Passive__c,Is_Promoter_ETS__c,Is_Detractor_ETS__c,Is_Passive_ETS__c,CES_Value__c,CES_Count__c
                                                            from Contact
                                                            where AccountId in :acctIds]);

    Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id
                                                                 ,RMT_Promoters_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);
    Map<ID, Account> acctsToUpdate2 = new Map<ID, Account>([select Id
                                                                 ,RMT_Detractors_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);    
    Map<ID, Account> acctsToUpdate3 = new Map<ID, Account>([select Id
                                                                 ,RMT_Passive_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);   
    Map<ID, Account> acctsToUpdate4 = new Map<ID, Account>([select Id
                                                                 ,ETS_Promoters_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);
    Map<ID, Account> acctsToUpdate5 = new Map<ID, Account>([select Id
                                                                 ,ETS_Detractors_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);    
    Map<ID, Account> acctsToUpdate6 = new Map<ID, Account>([select Id
                                                                 ,ETS_Passive_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]); 
    Map<ID, Account> acctsToUpdate7 = new Map<ID, Account>([select Id
                                                                 ,No_of_CES_response__c
                                                                  from Account
                                                                  where Id in :acctIds]);     
    Map<ID, Account> acctsToUpdate8 = new Map<ID, Account>([select Id
                                                                 ,Total_sum_of_CES_response__c
                                                                  from Account
                                                                  where Id in :acctIds]); 

	if(!acctsToUpdate.isEmpty())
	{
    for (Account acct : acctsToUpdate.values()) 
	{
        Set<ID> conIds = new Set<ID>();
        for (Contact con : contactsForAccounts.values()) 
		{
            if (con.AccountId == acct.Id && con.Is_Promoter__c == TRUE)
                conIds.add(con.Id);
        }
        if (acct.RMT_Promoters_Count__c != conIds.size())
            acct.RMT_Promoters_Count__c = conIds.size();
    }

    update acctsToUpdate.values();
    }  
	if(!acctsToUpdate2.isEmpty())
	{
    for (Account acct2 : acctsToUpdate2.values()) {
        Set<ID> conIds2 = new Set<ID>();
        for (Contact con2 : contactsForAccounts.values()) {
            if (con2.AccountId == acct2.Id && con2.Is_Detractor__c == TRUE)
                conIds2.add(con2.Id);
        }
        if (acct2.RMT_Detractors_Count__c != conIds2.size())
            acct2.RMT_Detractors_Count__c = conIds2.size();
    }
	
    update acctsToUpdate2.values();
    }
	if(!acctsToUpdate3.isEmpty())
	{
    for (Account acct3 : acctsToUpdate3.values()) {
        Set<ID> conIds3 = new Set<ID>();
        for (Contact con3 : contactsForAccounts.values()) {
            if (con3.AccountId == acct3.Id && con3.Is_Passive__c == TRUE)
                conIds3.add(con3.Id);
        }
        if (acct3.RMT_Passive_Count__c != conIds3.size())
            acct3.RMT_Passive_Count__c = conIds3.size();
    }

    update acctsToUpdate3.values();
    }
    if(!acctsToUpdate4.isEmpty())
	{
    for (Account acct4 : acctsToUpdate4.values()) {
        Set<ID> conIds4 = new Set<ID>();
        for (Contact con4 : contactsForAccounts.values()) {
            if (con4.AccountId == acct4.Id && con4.Is_Promoter_ETS__c == TRUE)
                conIds4.add(con4.Id);
        }
        if (acct4.ETS_Promoters_Count__c != conIds4.size())
            acct4.ETS_Promoters_Count__c = conIds4.size();
    }

    update acctsToUpdate4.values();
    }
	if(!acctsToUpdate5.isEmpty())
	{
    for (Account acct5 : acctsToUpdate5.values()) {
        Set<ID> conIds5 = new Set<ID>();
        for (Contact con5 : contactsForAccounts.values()) {
            if (con5.AccountId == acct5.Id && con5.Is_Detractor_ETS__c == TRUE)
                conIds5.add(con5.Id);
        }
        if (acct5.ETS_Detractors_Count__c != conIds5.size())
            acct5.ETS_Detractors_Count__c = conIds5.size();
    }

    update acctsToUpdate5.values();
    }
	if(!acctsToUpdate6.isEmpty())
	{
    for (Account acct6 : acctsToUpdate6.values()) {
        Set<ID> conIds6 = new Set<ID>();
        for (Contact con6 : contactsForAccounts.values()) {
            if (con6.AccountId == acct6.Id && con6.Is_Passive_ETS__c == TRUE)
                conIds6.add(con6.Id);
        }
        if (acct6.ETS_Passive_Count__c != conIds6.size())
            acct6.ETS_Passive_Count__c = conIds6.size();
    }

    update acctsToUpdate6.values();
    }
	if(!acctsToUpdate7.isEmpty())
	{
    for (Account acct7 : acctsToUpdate7.values()) {
        Set<ID> conIds7 = new Set<ID>();
        for (Contact con7 : contactsForAccounts.values()) {
            if (con7.AccountId == acct7.Id && con7.CES_Count__c != 0 )
                conIds7.add(con7.Id);
        }
        if (acct7.No_of_CES_response__c != conIds7.size())
            acct7.No_of_CES_response__c = conIds7.size();
    }
    
    update acctsToUpdate7.values();
    }
    
     Map<Id, Account> acc= new Map<Id, Account>([Select Id, Total_sum_of_CES_response__c From Account Where Id In :acctIds]);
    AggregateResult[] groupedResults = [SELECT AccountId,SUM(CES_Value__c)amt FROM contact where AccountId in:acctIds group by AccountId];
	if(!groupedResults.isEmpty())
	{
      for (AggregateResult ar : groupedResults ) {
        acc.get(String.valueOf(ar.get('Accountid'))).Total_sum_of_CES_response__c = Integer.valueOf(ar.get('amt'));
        accountsToUpdate.add(acc.get(String.valueOf(ar.get('Accountid'))));
    }
    update accountsToUpdate;    
    }
}



 
Danish HodaDanish Hoda
It's good, but you are again missing null checks at line #166.
SFDC Lightning 18SFDC Lightning 18
I give it can check on line no 160, if not can you please modified the code.
Danish HodaDanish Hoda
trigger CountCheckbox on Contact (after delete, after insert, after undelete,
after update) {

    Contact[] cons;
    if (Trigger.isDelete)
        cons = Trigger.old;
    else
        cons = Trigger.new;

    // get list of accounts
    Set<ID> acctIds = new Set<ID>();
    for (Contact con : cons) {
            acctIds.add(con.AccountId);
    }
    
    List<account> accountsToUpdate=new list<Account>();
   
   
    Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([select Id
                                                            ,AccountId,Is_Promoter__c,Is_Detractor__c,Is_Passive__c,Is_Promoter_ETS__c,Is_Detractor_ETS__c,Is_Passive_ETS__c,CES_Value__c,CES_Count__c
                                                            from Contact
                                                            where AccountId in :acctIds]);

    Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id
                                                                 ,RMT_Promoters_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);
    Map<ID, Account> acctsToUpdate2 = new Map<ID, Account>([select Id
                                                                 ,RMT_Detractors_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);    
    Map<ID, Account> acctsToUpdate3 = new Map<ID, Account>([select Id
                                                                 ,RMT_Passive_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);   
    Map<ID, Account> acctsToUpdate4 = new Map<ID, Account>([select Id
                                                                 ,ETS_Promoters_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);
    Map<ID, Account> acctsToUpdate5 = new Map<ID, Account>([select Id
                                                                 ,ETS_Detractors_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);    
    Map<ID, Account> acctsToUpdate6 = new Map<ID, Account>([select Id
                                                                 ,ETS_Passive_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]); 
    Map<ID, Account> acctsToUpdate7 = new Map<ID, Account>([select Id
                                                                 ,No_of_CES_response__c
                                                                  from Account
                                                                  where Id in :acctIds]);     
    Map<ID, Account> acctsToUpdate8 = new Map<ID, Account>([select Id
                                                                 ,Total_sum_of_CES_response__c
                                                                  from Account
                                                                  where Id in :acctIds]); 

    if(!acctsToUpdate.isEmpty())
    {
    for (Account acct : acctsToUpdate.values()) 
    {
        Set<ID> conIds = new Set<ID>();
        for (Contact con : contactsForAccounts.values()) 
        {
            if (con.AccountId == acct.Id && con.Is_Promoter__c == TRUE)
                conIds.add(con.Id);
        }
        if (acct.RMT_Promoters_Count__c != conIds.size())
            acct.RMT_Promoters_Count__c = conIds.size();
    }

    update acctsToUpdate.values();
    }  
    if(!acctsToUpdate2.isEmpty())
    {
    for (Account acct2 : acctsToUpdate2.values()) {
        Set<ID> conIds2 = new Set<ID>();
        for (Contact con2 : contactsForAccounts.values()) {
            if (con2.AccountId == acct2.Id && con2.Is_Detractor__c == TRUE)
                conIds2.add(con2.Id);
        }
        if (acct2.RMT_Detractors_Count__c != conIds2.size())
            acct2.RMT_Detractors_Count__c = conIds2.size();
    }
    
    update acctsToUpdate2.values();
    }
    if(!acctsToUpdate3.isEmpty())
    {
    for (Account acct3 : acctsToUpdate3.values()) {
        Set<ID> conIds3 = new Set<ID>();
        for (Contact con3 : contactsForAccounts.values()) {
            if (con3.AccountId == acct3.Id && con3.Is_Passive__c == TRUE)
                conIds3.add(con3.Id);
        }
        if (acct3.RMT_Passive_Count__c != conIds3.size())
            acct3.RMT_Passive_Count__c = conIds3.size();
    }

    update acctsToUpdate3.values();
    }
    if(!acctsToUpdate4.isEmpty())
    {
    for (Account acct4 : acctsToUpdate4.values()) {
        Set<ID> conIds4 = new Set<ID>();
        for (Contact con4 : contactsForAccounts.values()) {
            if (con4.AccountId == acct4.Id && con4.Is_Promoter_ETS__c == TRUE)
                conIds4.add(con4.Id);
        }
        if (acct4.ETS_Promoters_Count__c != conIds4.size())
            acct4.ETS_Promoters_Count__c = conIds4.size();
    }

    update acctsToUpdate4.values();
    }
    if(!acctsToUpdate5.isEmpty())
    {
    for (Account acct5 : acctsToUpdate5.values()) {
        Set<ID> conIds5 = new Set<ID>();
        for (Contact con5 : contactsForAccounts.values()) {
            if (con5.AccountId == acct5.Id && con5.Is_Detractor_ETS__c == TRUE)
                conIds5.add(con5.Id);
        }
        if (acct5.ETS_Detractors_Count__c != conIds5.size())
            acct5.ETS_Detractors_Count__c = conIds5.size();
    }

    update acctsToUpdate5.values();
    }
    if(!acctsToUpdate6.isEmpty())
    {
    for (Account acct6 : acctsToUpdate6.values()) {
        Set<ID> conIds6 = new Set<ID>();
        for (Contact con6 : contactsForAccounts.values()) {
            if (con6.AccountId == acct6.Id && con6.Is_Passive_ETS__c == TRUE)
                conIds6.add(con6.Id);
        }
        if (acct6.ETS_Passive_Count__c != conIds6.size())
            acct6.ETS_Passive_Count__c = conIds6.size();
    }

    update acctsToUpdate6.values();
    }
    if(!acctsToUpdate7.isEmpty())
    {
    for (Account acct7 : acctsToUpdate7.values()) {
        Set<ID> conIds7 = new Set<ID>();
        for (Contact con7 : contactsForAccounts.values()) {
            if (con7.AccountId == acct7.Id && con7.CES_Count__c != 0 )
                conIds7.add(con7.Id);
        }
        if (acct7.No_of_CES_response__c != conIds7.size())
            acct7.No_of_CES_response__c = conIds7.size();
    }
    
    update acctsToUpdate7.values();
    }
    
     Map<Id, Account> acc= new Map<Id, Account>([Select Id, Total_sum_of_CES_response__c From Account Where Id In :acctIds]);
    AggregateResult[] groupedResults = [SELECT AccountId,SUM(CES_Value__c)amt FROM contact where AccountId in:acctIds group by AccountId];
    if(!groupedResults.isEmpty())
    {
      for (AggregateResult ar : groupedResults ) {
        acc.get(String.valueOf(ar.get('Accountid'))).Total_sum_of_CES_response__c = Integer.valueOf(ar.get('amt'));
        accountsToUpdate.add(acc.get(String.valueOf(ar.get('Accountid'))));
    }
    if(!accountsToUpdate.isEmpty())    update accountsToUpdate;    
    }
}
SFDC Lightning 18SFDC Lightning 18
Hi Danish, I pasted same above code but still I am getting same issue.

CountCheckbox : CountCheckbox: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object ()
Danish HodaDanish Hoda
trigger CountCheckbox on Contact (after delete, after insert, after undelete,
after update) {

    //Contact[] cons;
    list<Contact> cons = new List<Contact>();
    if (Trigger.isDelete)
        cons = Trigger.old;
    else
        cons = Trigger.new;

    // get list of accounts
    Set<ID> acctIds = new Set<ID>();
    if(!cons.isEmpty()){
    for (Contact con : cons) {
            acctIds.add(con.AccountId);
    }
   }
    
    List<account> accountsToUpdate=new list<Account>();
   
   
    Map<ID, Contact> contactsForAccounts = new Map<ID, Contact>([select Id
                                                            ,AccountId,Is_Promoter__c,Is_Detractor__c,Is_Passive__c,Is_Promoter_ETS__c,Is_Detractor_ETS__c,Is_Passive_ETS__c,CES_Value__c,CES_Count__c
                                                            from Contact
                                                            where AccountId in :acctIds]);

    Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id
                                                                 ,RMT_Promoters_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);
    Map<ID, Account> acctsToUpdate2 = new Map<ID, Account>([select Id
                                                                 ,RMT_Detractors_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);    
    Map<ID, Account> acctsToUpdate3 = new Map<ID, Account>([select Id
                                                                 ,RMT_Passive_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);   
    Map<ID, Account> acctsToUpdate4 = new Map<ID, Account>([select Id
                                                                 ,ETS_Promoters_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);
    Map<ID, Account> acctsToUpdate5 = new Map<ID, Account>([select Id
                                                                 ,ETS_Detractors_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]);    
    Map<ID, Account> acctsToUpdate6 = new Map<ID, Account>([select Id
                                                                 ,ETS_Passive_Count__c
                                                                  from Account
                                                                  where Id in :acctIds]); 
    Map<ID, Account> acctsToUpdate7 = new Map<ID, Account>([select Id
                                                                 ,No_of_CES_response__c
                                                                  from Account
                                                                  where Id in :acctIds]);     
    Map<ID, Account> acctsToUpdate8 = new Map<ID, Account>([select Id
                                                                 ,Total_sum_of_CES_response__c
                                                                  from Account
                                                                  where Id in :acctIds]); 

	if(!acctsToUpdate.isEmpty())
	{
    for (Account acct : acctsToUpdate.values()) 
	{
        Set<ID> conIds = new Set<ID>();
        for (Contact con : contactsForAccounts.values()) 
		{
            if (con.AccountId == acct.Id && con.Is_Promoter__c == TRUE)
                conIds.add(con.Id);
        }
        if (acct.RMT_Promoters_Count__c != conIds.size())
            acct.RMT_Promoters_Count__c = conIds.size();
    }

    update acctsToUpdate.values();
    }  
	if(!acctsToUpdate2.isEmpty())
	{
    for (Account acct2 : acctsToUpdate2.values()) {
        Set<ID> conIds2 = new Set<ID>();
        for (Contact con2 : contactsForAccounts.values()) {
            if (con2.AccountId == acct2.Id && con2.Is_Detractor__c == TRUE)
                conIds2.add(con2.Id);
        }
        if (acct2.RMT_Detractors_Count__c != conIds2.size())
            acct2.RMT_Detractors_Count__c = conIds2.size();
    }
	
    update acctsToUpdate2.values();
    }
	if(!acctsToUpdate3.isEmpty())
	{
    for (Account acct3 : acctsToUpdate3.values()) {
        Set<ID> conIds3 = new Set<ID>();
        for (Contact con3 : contactsForAccounts.values()) {
            if (con3.AccountId == acct3.Id && con3.Is_Passive__c == TRUE)
                conIds3.add(con3.Id);
        }
        if (acct3.RMT_Passive_Count__c != conIds3.size())
            acct3.RMT_Passive_Count__c = conIds3.size();
    }

    update acctsToUpdate3.values();
    }
    if(!acctsToUpdate4.isEmpty())
	{
    for (Account acct4 : acctsToUpdate4.values()) {
        Set<ID> conIds4 = new Set<ID>();
        for (Contact con4 : contactsForAccounts.values()) {
            if (con4.AccountId == acct4.Id && con4.Is_Promoter_ETS__c == TRUE)
                conIds4.add(con4.Id);
        }
        if (acct4.ETS_Promoters_Count__c != conIds4.size())
            acct4.ETS_Promoters_Count__c = conIds4.size();
    }

    update acctsToUpdate4.values();
    }
	if(!acctsToUpdate5.isEmpty())
	{
    for (Account acct5 : acctsToUpdate5.values()) {
        Set<ID> conIds5 = new Set<ID>();
        for (Contact con5 : contactsForAccounts.values()) {
            if (con5.AccountId == acct5.Id && con5.Is_Detractor_ETS__c == TRUE)
                conIds5.add(con5.Id);
        }
        if (acct5.ETS_Detractors_Count__c != conIds5.size())
            acct5.ETS_Detractors_Count__c = conIds5.size();
    }

    update acctsToUpdate5.values();
    }
	if(!acctsToUpdate6.isEmpty())
	{
    for (Account acct6 : acctsToUpdate6.values()) {
        Set<ID> conIds6 = new Set<ID>();
        for (Contact con6 : contactsForAccounts.values()) {
            if (con6.AccountId == acct6.Id && con6.Is_Passive_ETS__c == TRUE)
                conIds6.add(con6.Id);
        }
        if (acct6.ETS_Passive_Count__c != conIds6.size())
            acct6.ETS_Passive_Count__c = conIds6.size();
    }

    update acctsToUpdate6.values();
    }
	if(!acctsToUpdate7.isEmpty())
	{
    for (Account acct7 : acctsToUpdate7.values()) {
        Set<ID> conIds7 = new Set<ID>();
        for (Contact con7 : contactsForAccounts.values()) {
            if (con7.AccountId == acct7.Id && con7.CES_Count__c != 0 )
                conIds7.add(con7.Id);
        }
        if (acct7.No_of_CES_response__c != conIds7.size())
            acct7.No_of_CES_response__c = conIds7.size();
    }
    
    update acctsToUpdate7.values();
    }
    
     Map<Id, Account> acc= new Map<Id, Account>([Select Id, Total_sum_of_CES_response__c From Account Where Id In :acctIds]);
    AggregateResult[] groupedResults = [SELECT AccountId,SUM(CES_Value__c)amt FROM contact where AccountId in:acctIds group by AccountId];
	if(!groupedResults.isEmpty())
	{
      for (AggregateResult ar : groupedResults ) {
        acc.get(String.valueOf(ar.get('Accountid'))).Total_sum_of_CES_response__c = Integer.valueOf(ar.get('amt'));
        accountsToUpdate.add(acc.get(String.valueOf(ar.get('Accountid'))));
    }
    update accountsToUpdate;    
    }
}