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
MaheemSamMaheemSam 

How to add or sum boolean values in code

Hi,

  I have a below code which is working as expected it gives list of account names and contact name top to bottom

  Now my requirement is in contact we have NSE1 and NSE2 has two feilds  this needs to be added or sum and displayed when it is views from partner account
 
Example;  
        Account A is Top Parent  Has 1 Contacts  NSE1 = 1 and NSE2 = 1
        Account  B is Child of Account A Has 1 Contacts  NSE1= 1 and NSE2 = 1
        Account C is Child of Account B  Has 1 Contacts  NSE1= 0 and NSE2 = 1

If I see Account A Contact NSE1 and NSE2 should be NSE1 = 2 and NSE2 = 3
If I see Account A Contact NSE1 and NSE2 should be NSE1 = 1 and NSE2 = 2
If I see Account A Contact NSE1 and NSE2 should be NSE1 = 0 and NSE2 = 1

Code should display values as above. Please suggest me how to obtain this sum or add up the boolean values at parent level.
set<id> setactid = new set<id>();
    
Id accountId = '001W000000aDqeA';
        
 Account[] allparents = new Account[] {};
            
 Set<Id> parentIds = new Set<Id>{accountId};
                
 Account[] parent;
        
 do {
            
  parent = [select Id,ParentId, Name from Account where Id in :parentIds];
            
  allparents.addAll(parent);
            
  parentIds.clear();
            
  for (Account par : parent) 
                
  parentIds.add(par.ParentId);
            
  } while (parent.size() > 0);
        
  list<Account> Act = [select id, name from account where id in :allparents];
        
  for(Account A : Act){
     
    system.debug(a.name);  
     setactid.add(a.id);
           //dispaly here the sum of NSE1 and NSE2 account level grouping the contacts

      for(Contact C : [select id,name,NSE_1__c,NSE_2__c from contact where accountid in :setactid]){
        system.debug(c.name);
          system.debug('NSE_1__c   ' + c.NSE_1__c); //How to add this boolean values 
          system.debug('NSE_2__c   ' + c.NSE_2__c);
          
      }  
            
   }


Thanks
Sudhir


 
Best Answer chosen by MaheemSam
Gokula KrishnanGokula Krishnan
Hi Sudhir, 

Try this, Hope it works fine.
 
trigger ContactTrigger on Contact  (before insert, before update, After insert, after update, After Delete, After UnDelete) {

  if(trigger.isInsert &&Trigger.isAfter){
    ContactCertificateRollupHandlerNew.accountRollupInsert(Trigger.new);
  }

 If(trigger.isUpdate &&Trigger.isAfter) {
   ContactCertificateRollupHandlerNew.accountRollupInsert(Trigger.new);
   }
}



// Apex Class : ContactCertificateRollupHandlerNew
public class ContactCertificateRollupHandlerNew{

Public static Map<id,integer> NSEmap1;
Public static Map<id,integer> NSEmap2;

Public static Integer NSEint1 = 0;
Public static Integer NSEint2 = 0;

Public List<account> NSE1_Update = new List<account>();
Public List<account> NSE2_Update = new List<account>();


public static void accountRollupInsert(List<Contact> newLst) {
        list<id> actidSet = new list<id>();
         NSEmap1 = new Map<id,integer>();
         NSEmap2 = new Map<id,integer>();
 
        for(Contact cont : newLst){
            ChildToParnet(cont.AccountId);
        }
        
       
         if(NSEmap1.size()>0){
    
            For(Id i : NSEmap1.keyset()){        
                Account ains = new Account(); 
                ains.id = i;
                ains.NSE_1_Status__c = NSEmap1.get(i);
                NSE1_Update.add(ains);    
            }
        }
          
        
           if(NSEmap2.size()>0){
    
           For(Id i : NSEmap2.keyset()){        
				Account ains = new Account();        
				ains.id = i;
				ains.NSE_2_Status__c = NSEmap2.get(i);        
				NSE2_Update.add(ains);
            }
    
         }
         
           if(NSE1_Update.size()>0)
             update NSE1_Update;



          if(NSE2_Update.size()>0)
            update NSE2_Update;  

        
       
    }

public static void ChildToParnet (Id ChildId){
     
    For(Account acc : [select id,name, parentid, (Select id,NSE_1__c,NSE_2__c from contacts)from account where id =: ChildId]){
        
        For(Contact c : acc.contacts ){
            
            if(c.NSE_1__c == true)               
                 NSEint1 = NSEint1 +1;
                 NSEmap1.put(acc.id,NSEint1);
                 
            
            if(c.NSE_2__c == true)
                NSEint2 = NSEint2 + 1;                          
                NSEmap2.put(acc.id,NSEint2);              
            
        }
        
        if(acc.parentid != null)            
           childToParnet(acc.parentid);
        
     }
  }
}
Thanks!!!

If it helps you, please mark is as best answer, so it will be helpful for other developers.
 

All Answers

Gokula KrishnanGokula Krishnan
Hi Sudhir,

You can use If Condition 
Eg:
Integer NSE = 0;
if(NSE_1__c   )
   NSE = NSE +  1;

From your code, Line 03 is Child Account ID. So, are you trying to get data from Child - to - Account..?

Thanks!!!
MaheemSamMaheemSam
HI Gokul,

   Yes I am trying to get data from child to account, can you suggest me how to group the child and get account wise.

Thanks
Sudhir
Gokula KrishnanGokula Krishnan
Hi ,

Try this,
 
//Create Two integer Fields in Account NSE_1__c and NSE_2__c, Once the programs runs you check this field value in //Account level

Public Map<id,integer> NSEmap1;
Public Map<id,integer> NSEmap2;

Public Integer NSEint1 = 0;
Public Integer NSEint2 = 0;


Public void MainMeth(){
	Id accountId = '001W000000aDqeA';
	NSEmap1 = new Map<id,integer>();
	NSEmap2	= new Map<id,integer>();
	ChildToParnet(accountId);
	List<account> NSE1_Update = new List<account>();
	List<account> NSE2_Update = new List<account>();
	
	if(NSEmap1.size()>0){
		For(Id i : NSEmap1.keyset()){
			Account ains = new Account()
			ains.id = i;
			ains.NSE_1__c = NSEmap1.get(i);
			NSE1_Update.add(ains);
		}
	}
	
	if(NSEmap2.size()>0){
		For(Id i : NSEmap2.keyset()){
			Account ains = new Account()
			ains.id = i;
			ains.NSE_2__c = NSEmap2.get(i);
			NSE2_Update.add(ains);
		}
	}
	
	if(NSE1_Update.size()>0)
		update NSE1_Update;
	
	if(NSE2_Update.size()>0)
		update NSE2_Update;	
		
}

Public void ChildToParnet (Id ChildId){

	For(Account acc : [select id,name, parentid, (Select id,NSE_1__c,NSE_2__c from contacts)from account where id =: ChildId]){
		For(Contact c : acc.contacts ){
			if(c.NSE_1__c)
				NSEmap1.put(acc.id,NSEint1 + 1);
			if(c.NSE_2__c)
				NSEmap2.put(acc.id,NSEint2 + 1);
		}
		if(acc.parentid != null)
			ChildToParnet(acc.parentid);
	}
	
}

Thanks!!!!

If it helps you, please mark is as best answer, so it will be helpful for other developers.
MaheemSamMaheemSam
Thanks Gokula I tired but its not rollup the sum contacts account wise can you please add some system.debug and show me how it is working
Gokula KrishnanGokula Krishnan
Hi Sudhir,

Add System.debug("Acc ID==>"+i+"NSE -1==>"+NSEmap1.get(i)); // in Line 23
Add System.debug("Acc ID==>"+i+"NSE -2==>"+NSEmap2.get(i)); // in Line 32

You will get which account and its NSE values.

Thanks..!!!
MaheemSamMaheemSam
Hi Gokul, 
   
    Thanks very much for your help one part is not working looks like NSE1 AND NSE2 is not grouping like blow please see below example

   Parent Account - 1 (Account level NSE1 = 5 and NSE2=4)
   Have 2 Contacts with NSE1 = 1 AND NSE2= 1

   Child Account - 2 (Parent Account-1)   (Account level NSE1 = 4 and NSE2=3)
    Have 2 Contacts with NSE1 = 2 AND NSE2= 1

   Sub Child Acount - 3  (Child Account - 2)      (Account level NSE1 = 2 and NSE2=2)
    Have 2 Contacts with NSE1 = 2 AND NSE2= 2

At account level NSE1 AND NSE2 should populate as how it is poopulated let me know if this is not clear i can explain in another example 

   Thanks for your help your genious 

Thanks
Sudhir

 
Gokula KrishnanGokula Krishnan
Hi Sudhir,

What output are you getting..?

and 

Is NSE1 and NSE2 are Boolean Values..?

I understood your example.

Thanks!!!
MaheemSamMaheemSam
Its comming as 1 and 1 but not the summarion or group wise 
Gokula KrishnanGokula Krishnan
Hi,

I miss this.

Replace Line 47 - 52 as below
For(Contact c : acc.contacts ){
			if(c.NSE_1__c){
                                NSEint1 = NSEint1 + 1
				NSEmap1.put(acc.id,NSEint1);
			}
                        if(c.NSE_2__c){
                                NSEint2 = NSEint2 + 1;
				NSEmap2.put(acc.id,NSEint2 );
		        }
}
Hope it works now.

Thanks!!!

If it helps you, please mark is as best answer, so it will be helpful for other developers.
MaheemSamMaheemSam
Thanks Gokul, After adding the new line as you suggested I am getting below error I am doing this in developer console 

Line: 45, Column: 7
Unexpected token 'ChildToParnet'.

 
//Create Two integer Fields in Account NSE_1__c and NSE_2__c, Once the programs runs you check this field value in //Account level

Public Map<id,integer> NSEmap1;
Public Map<id,integer> NSEmap2;

Public Integer NSEint1 = 0;
Public Integer NSEint2 = 0;

	Id accountId = '001W000000aDqeA';
	NSEmap1 = new Map<id,integer>();
	NSEmap2	= new Map<id,integer>();
	ChildToParnet(accountId);
    
	List<account> NSE1_Update = new List<account>();
	List<account> NSE2_Update = new List<account>();
	
	if(NSEmap1.size()>0){
		For(Id i : NSEmap1.keyset()){
			Account ains = new Account();
			ains.id = i;
             system.debug(NSEmap1.get(i));
			ains.NSE_1_Status__c = NSEmap1.get(i);
			NSE1_Update.add(ains);
		}
	}
	
	if(NSEmap2.size()>0){
		For(Id i : NSEmap2.keyset()){
			Account ains = new Account();
			ains.id = i;
            system.debug(NSEmap2.get(i));
			ains.NSE_2_Status__c = NSEmap2.get(i);
			NSE2_Update.add(ains);
		}
	}
	
	if(NSE1_Update.size()>0)
		update NSE1_Update;
	
	if(NSE2_Update.size()>0)
		update NSE2_Update;	
		


 void ChildToParnet (Id ChildId){
	For(Account acc : [select id,name, parentid, (Select id,NSE_1__c,NSE_2__c from contacts)from account where id =: ChildId]){
		For(Contact c : acc.contacts ){
			if(c.NSE_1__c){
                                NSEint1 = NSEint1 + 1
				NSEmap1.put(acc.id,NSEint1);
			}
                        if(c.NSE_2__c){
                                NSEint2 = NSEint2 + 1;
				NSEmap2.put(acc.id,NSEint2 );
		        }
}
		if(acc.parentid != null)
			ChildToParnet(acc.parentid);
	}
	
}

Thanks
Sudhir
MaheemSamMaheemSam
Hi Gokul, 

  Please ignor my previous msg its working Thanks Buddy you solved my issue I am doing some more testing. 

Thanks
Sudhir
Gokula KrishnanGokula Krishnan
You con't run in Developer Console, you need to create a Apex Class and execute it.
Thanks!!!
MaheemSamMaheemSam
Hi Gokula,

    I was doing testing on your code there seems to be one bug

    If I am doing for top parent

   Account - 1

   Has only one Contact with NSE1 = true and NSE2  = true

   When I run your code it is updating as value in Account NSE1 = 2 and NSE2 = 2 Please check and let me know

Thanks very much for your help

Thanks
Sudhir







 
MaheemSamMaheemSam
Hi Gokul,

   I fixed above it is working I need you suggestion for insert now the code is working as expected for update Please suggest me.

 
public class ContactCertificateRollupHandlerNew{

Public static Map<id,integer> NSEmap1;
Public static Map<id,integer> NSEmap2;

Public static Integer NSEint1 = 0;
Public static Integer NSEint2 = 0;

  public static void accountRollup(List<Contact> newLst) {
          
        list<id> actidSet = new list<id>();

        for(Contact cont : newLst){
                actidSet.add(cont.AccountId);
        }
        
        Id accountId = actidSet[0];
        
        NSEmap1 = new Map<id,integer>();
        NSEmap2 = new Map<id,integer>();

        ChildToParnet(accountId);

        List<account> NSE1_Update = new List<account>();
        List<account> NSE2_Update = new List<account>();

        if(NSEmap1.size()>0){
    
            For(Id i : NSEmap1.keyset()){        
                Account ains = new Account(); 
                ains.id = i;
        
                system.debug('Account' + i);       
                system.debug(NSEmap1.get(i));
        
                ains.NSE_1_Status__c = NSEmap1.get(i);
                NSE1_Update.add(ains);    
            }
        }



       if(NSEmap2.size()>0){
    
           For(Id i : NSEmap2.keyset()){        
               Account ains = new Account();        
               ains.id = i;
        
              system.debug('Account' + i);        
              system.debug(NSEmap2.get(i));
        
              ains.NSE_2_Status__c = NSEmap2.get(i);        
              NSE2_Update.add(ains);
        
    }
    
}



if(NSE1_Update.size()>0)
    
    update NSE1_Update;



if(NSE2_Update.size()>0)
    
    update NSE2_Update;  


        
   }



public static void ChildToParnet (Id ChildId){
     
    For(Account acc : [select id,name, parentid, (Select id,NSE_1__c,NSE_2__c from contacts)from account where id =: ChildId]){
        
        For(Contact c : acc.contacts ){
            
            if(c.NSE_1__c == true)               
                 NSEint1 = NSEint1 +1;
                 NSEmap1.put(acc.id,NSEint1);
                 
            
            if(c.NSE_2__c == true)
                NSEint2 = NSEint2 + 1;                          
                NSEmap2.put(acc.id,NSEint2);
              
            
        }
        
        if(acc.parentid != null)            
           childToParnet(acc.parentid);
        
     }
  }
}
Thanks
Sudhir
 
Gokula KrishnanGokula Krishnan

Hi Sudhir,

Which object are plan to insert..?

and what field values to be inserted in that object.

Thanks!!!

MaheemSamMaheemSam

Hi Gokul,

    It will be same logic In account I will be creating new conact with NSE1 = true and NSE2  = true this will update to the existing values.

   Also Please let me know how to perform same for delete if any contact is deleted it must reduce Please let me know if you understand else will give in a example

  Update is working fine with abvove code.
 

Thanks
Sudhir

Gokula KrishnanGokula Krishnan
H Sudhir,

Try this,
 
public class ContactCertificateRollupHandlerNew{

Public static Map<id,integer> NSEmap1;
Public static Map<id,integer> NSEmap2;

Public static Integer NSEint1 = 0;
Public static Integer NSEint2 = 0;

  public static void accountRollup(List<Contact> newLst) {
          
        list<id> actidSet = new list<id>();

        for(Contact cont : newLst){
                actidSet.add(cont.AccountId);
        }
        
        Id accountId = actidSet[0];
        
        NSEmap1 = new Map<id,integer>();
        NSEmap2 = new Map<id,integer>();

        ChildToParnet(accountId);

        List<account> NSE1_Update = new List<account>();
        List<account> NSE2_Update = new List<account>();
		
		List<contact> ConInsert = new List<contact>();

        if(NSEmap1.size()>0){
    
            For(Id i : NSEmap1.keyset()){        
                Account ains = new Account(); 
                ains.id = i;
        
                system.debug('Account' + i);       
                system.debug(NSEmap1.get(i));
        
                ains.NSE_1_Status__c = NSEmap1.get(i);
                NSE1_Update.add(ains);    
            }
        }



       if(NSEmap2.size()>0){
    
           For(Id i : NSEmap2.keyset()){        
               Account ains = new Account();        
               ains.id = i;
        
              system.debug('Account' + i);        
              system.debug(NSEmap2.get(i));
        
              ains.NSE_2_Status__c = NSEmap2.get(i);        
              NSE2_Update.add(ains);
			  
			  contact c = new contact();
			  c.accountid = i;
			  c.NSE2__c =True;
			  if(NSEmap1.containskey(i))
			  c.NSE1__c= True;
			  ConInsert.add(c);
        
    }
    
}



if(NSE1_Update.size()>0)
    
    update NSE1_Update;



if(NSE2_Update.size()>0)
    
    update NSE2_Update;  

if(ConInsert.size()>0)
	Insert ConInsert;//contact inserted
        
   }



public static void ChildToParnet (Id ChildId){
     
    For(Account acc : [select id,name, parentid, (Select id,NSE_1__c,NSE_2__c from contacts)from account where id =: ChildId]){
        
        For(Contact c : acc.contacts ){
            
            if(c.NSE_1__c == true)               
                 NSEint1 = NSEint1 +1;
                 NSEmap1.put(acc.id,NSEint1);
                 
            
            if(c.NSE_2__c == true)
                NSEint2 = NSEint2 + 1;                          
                NSEmap2.put(acc.id,NSEint2);
              
            
        }
        
        if(acc.parentid != null)            
           childToParnet(acc.parentid);
        
     }
  }
}

For Deletions, you need to go Trigger.

Thanks..

 
MaheemSamMaheemSam
Hi Gokul,

   I do not want insert code of contact all I need is when a new record is inserted (Contact) NSE1 and NSE2 value should rollup to account NSE1 and NSE2 Please find the trigger logic how i am using I think we must do a after update with existing account NSE1 NSE2 to new contact NSE1 AND NSE2
 
trigger ContactTrigger on Contact  (before insert, before update, After insert, after update, After Delete, After UnDelete) {

  if(trigger.isInsert){
     // call the simialar logic to update 
  }

 If(trigger.isUpdate) {
   ContactCertificateRollupHandlerNew.accountRollup(Trigger.new);  
   }


}
Please let me know if your not clear

Thanks
Sudhir
 
Gokula KrishnanGokula Krishnan
Try this,
 
trigger ContactTrigger on Contact  (before insert, before update, After insert, after update, After Delete, After UnDelete) {

  if(trigger.isInsert &&Trigger.isAfter){
    ContactCertificateRollupHandlerNew.accountRollup(Trigger.new);
  }

 If(trigger.isUpdate &&Trigger.isAfter) {
   ContactCertificateRollupHandlerNew.accountRollup(Trigger.new);  
   }


}

Thank!!!
MaheemSamMaheemSam
Hi Gokula,

    Let me explain you again What I am trying to do is just update the account NSE1 and NSE2 with new contact NSE1 and NSE2 Please find code below
  
   Trigger
trigger ContactTrigger on Contact  (before insert, before update, After insert, after update, After Delete, After UnDelete) {

 if (Trigger.isUpdate) {

  if(trigger.isInsert &&Trigger.isAfter){
    ContactCertificateRollupHandlerNew.accountRollupInsert(Trigger.new);
  }

 If(trigger.isUpdate &&Trigger.isAfter) {
   ContactCertificateRollupHandlerNew.accountRollupUpdate(Trigger.new);
   }

}
}

Helper Class
public class ContactCertificateRollupHandlerNew{

Public static Map<id,integer> NSEmap1;
Public static Map<id,integer> NSEmap2;

Public static Integer NSEint1 = 0;
Public static Integer NSEint2 = 0;

Public static List<account> NSE1_Update = new List<account>();
Public static List<account> NSE2_Update = new List<account>();


public static void accountRollupInsert(List<Contact> newLst) {
        list<id> actidSet = new list<id>();
         NSEmap1 = new Map<id,integer>();
         NSEmap2 = new Map<id,integer>();
 
        for(Contact cont : newLst){
            actidSet.add(cont.AccountId);
            
            if(cont.NSE_1__c == true)               
                 NSEint1 = NSEint1 +1;
                 NSEmap1.put(cont.AccountId,NSEint1);
                 
            
            if(cont.NSE_2__c == true)
                NSEint2 = NSEint2 + 1;                          
                NSEmap2.put(cont.AccountId,NSEint2);
        }
        
       
         if(NSEmap1.size()>0){
    
            For(Id i : NSEmap1.keyset()){        
                Account ains = new Account(); 
                ains.id = i;
        
                system.debug('Account' + i);       
                system.debug(NSEmap1.get(i));
        
                ains.NSE_1_Status__c = ains.NSE_1_Status__c + NSEmap1.get(i);
                NSE1_Update.add(ains);    
            }
        }
          
        
           if(NSEmap2.size()>0){
    
           For(Id i : NSEmap2.keyset()){        
               Account ains = new Account();        
               ains.id = i;
        
              system.debug('Account' + i);        
              system.debug(NSEmap2.get(i));
        
              ains.NSE_2_Status__c = ains.NSE_2_Status__c + NSEmap2.get(i);        
              NSE2_Update.add(ains);
        
            }
    
         }
         
           if(NSE1_Update.size()>0)
    
             update NSE1_Update;



          if(NSE2_Update.size()>0)
    
            update NSE2_Update;  

        
       
    }



            
public static void accountRollupUpdate(List<Contact> newLst) {
          
        list<id> actidSet = new list<id>();

        for(Contact cont : newLst){
                actidSet.add(cont.AccountId);
        }
        
        Id accountId = actidSet[0];
        
        NSEmap1 = new Map<id,integer>();
        NSEmap2 = new Map<id,integer>();

        ChildToParnet(accountId);


        if(NSEmap1.size()>0){
    
            For(Id i : NSEmap1.keyset()){        
                Account ains = new Account(); 
                ains.id = i;
        
                system.debug('Account' + i);       
                system.debug(NSEmap1.get(i));
        
                ains.NSE_1_Status__c = NSEmap1.get(i);
                NSE1_Update.add(ains);    
            }
        }


       if(NSEmap2.size()>0){
    
           For(Id i : NSEmap2.keyset()){        
               Account ains = new Account();        
               ains.id = i;
        
              system.debug('Account' + i);        
              system.debug(NSEmap2.get(i));
        
              ains.NSE_2_Status__c = NSEmap2.get(i);        
              NSE2_Update.add(ains);
        
    }
    
}

        if(NSE1_Update.size()>0)
    
          update NSE1_Update;


        if(NSE2_Update.size()>0)
    
            update NSE2_Update;  


        
   }



public static void ChildToParnet (Id ChildId){
     
    For(Account acc : [select id,name, parentid, (Select id,NSE_1__c,NSE_2__c from contacts)from account where id =: ChildId]){
        
        For(Contact c : acc.contacts ){
            
            if(c.NSE_1__c == true)               
                 NSEint1 = NSEint1 +1;
                 NSEmap1.put(acc.id,NSEint1);
                 
            
            if(c.NSE_2__c == true)
                NSEint2 = NSEint2 + 1;                          
                NSEmap2.put(acc.id,NSEint2);              
            
        }
        
        if(acc.parentid != null)            
           childToParnet(acc.parentid);
        
     }
  }
}
Sorry code is not aligned But I am having issue when contact insert is made it must update the account
I am getting below error now.

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ContactTrigger caused an unexpected exception, contact your administrator: ContactTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: ()


Thanks
Sudhir

 
Gokula KrishnanGokula Krishnan
Hi Sudhir, 

Try this, Hope it works fine.
 
trigger ContactTrigger on Contact  (before insert, before update, After insert, after update, After Delete, After UnDelete) {

  if(trigger.isInsert &&Trigger.isAfter){
    ContactCertificateRollupHandlerNew.accountRollupInsert(Trigger.new);
  }

 If(trigger.isUpdate &&Trigger.isAfter) {
   ContactCertificateRollupHandlerNew.accountRollupInsert(Trigger.new);
   }
}



// Apex Class : ContactCertificateRollupHandlerNew
public class ContactCertificateRollupHandlerNew{

Public static Map<id,integer> NSEmap1;
Public static Map<id,integer> NSEmap2;

Public static Integer NSEint1 = 0;
Public static Integer NSEint2 = 0;

Public List<account> NSE1_Update = new List<account>();
Public List<account> NSE2_Update = new List<account>();


public static void accountRollupInsert(List<Contact> newLst) {
        list<id> actidSet = new list<id>();
         NSEmap1 = new Map<id,integer>();
         NSEmap2 = new Map<id,integer>();
 
        for(Contact cont : newLst){
            ChildToParnet(cont.AccountId);
        }
        
       
         if(NSEmap1.size()>0){
    
            For(Id i : NSEmap1.keyset()){        
                Account ains = new Account(); 
                ains.id = i;
                ains.NSE_1_Status__c = NSEmap1.get(i);
                NSE1_Update.add(ains);    
            }
        }
          
        
           if(NSEmap2.size()>0){
    
           For(Id i : NSEmap2.keyset()){        
				Account ains = new Account();        
				ains.id = i;
				ains.NSE_2_Status__c = NSEmap2.get(i);        
				NSE2_Update.add(ains);
            }
    
         }
         
           if(NSE1_Update.size()>0)
             update NSE1_Update;



          if(NSE2_Update.size()>0)
            update NSE2_Update;  

        
       
    }

public static void ChildToParnet (Id ChildId){
     
    For(Account acc : [select id,name, parentid, (Select id,NSE_1__c,NSE_2__c from contacts)from account where id =: ChildId]){
        
        For(Contact c : acc.contacts ){
            
            if(c.NSE_1__c == true)               
                 NSEint1 = NSEint1 +1;
                 NSEmap1.put(acc.id,NSEint1);
                 
            
            if(c.NSE_2__c == true)
                NSEint2 = NSEint2 + 1;                          
                NSEmap2.put(acc.id,NSEint2);              
            
        }
        
        if(acc.parentid != null)            
           childToParnet(acc.parentid);
        
     }
  }
}
Thanks!!!

If it helps you, please mark is as best answer, so it will be helpful for other developers.
 
This was selected as the best answer
MaheemSamMaheemSam
Wonderfull Gokulan it worked as expected can you help me the delete part when deleted any contact it must reduce the count in account and parent account.

Thanks
Sudhir
MaheemSamMaheemSam
Hi Gokulan,

   Need your help finally I test only the update part it was working I am trying to create a new contact it is giving me below error from you code.

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger ContactTrigger caused an unexpected exception, contact your administrator: ContactTrigger: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: 001W000000aDqeAIAS: Class.ContactCertificateRollupHandlerNew.accountRollupInsert: line 46, column 1". 


Also please help me with Delete part Thanks for you help.

Thanks
Sudhir
 
MaheemSamMaheemSam
Hi Gokulan,

   I wrote different methods for Insert Update and Delete,

  Insert and Delete is working fine without any issue Please suggest me how to add the Delete method I am trying to update all contacts of parent after the contact is is deleted it is not working
public class ContactCertificateRollupHandlerNew{

Public static Map<id,integer> NSEmap1;
Public static Map<id,integer> NSEmap2;

Public static Integer NSEint1 = 0;
Public static Integer NSEint2 = 0;

Public static List<account> NSE1_Update = new List<account>();
Public static List<account> NSE2_Update = new List<account>();


public static void accountRollupInsert(List<Contact> newLst) {
        list<id> actidSet = new list<id>();
 
        for(Contact cont : newLst){
            actidSet.add(cont.AccountId);                                 
        }    
   
    }


public static void accountRollupDelete(List<Contact> newLst) {
        list<id> actidSet = new list<id>();
        list <account> act = new list <account>();
        list<contact> cnt = new list <contact>();
        
        for(Contact cont : newLst){
            actidSet.add(cont.AccountId);                                    
        }        
        
                      
        for(Account ac: [select id,parentid,NSE_1_Status__c,NSE_2_Status__c from account where id in :actidSet]){            
           if(ac.parentid <> null){
              for(contact cnts : [select id,NSE_1__c,NSE_2__c from contact where accountid = :ac.parentid]){
                 cnt.add(cnts);
               }
           }
        } 
         
            map<id,contact> cntmap = new map<id,contact>();
            cntmap.putall(cnt);
        
            if(cntmap.size()>0){
            update cntmap.values();         
            }
            
            map<id,account> accmap = new map<id,account>();
            accmap.putall(act);
        
            if(accmap.size()>0){
            update accmap.values();
            }        
      
    }
            
public static void accountRollupUpdate(List<Contact> newLst) {
          
        list<id> actidSet = new list<id>();

        for(Contact cont : newLst){
                actidSet.add(cont.AccountId);
        }
        
        Id accountId = actidSet[0];
        
        NSEmap1 = new Map<id,integer>();
        NSEmap2 = new Map<id,integer>();

        ChildToParnet(accountId);


        if(NSEmap1.size()>0){
    
            For(Id i : NSEmap1.keyset()){        
                Account ains = new Account(); 
                ains.id = i;
        
                system.debug('Account' + i);       
                system.debug(NSEmap1.get(i));
        
                ains.NSE_1_Status__c = NSEmap1.get(i);
                NSE1_Update.add(ains);    
            }
        }


       if(NSEmap2.size()>0){
    
           For(Id i : NSEmap2.keyset()){        
               Account ains = new Account();        
               ains.id = i;
        
              system.debug('Account' + i);        
              system.debug(NSEmap2.get(i));
        
              ains.NSE_2_Status__c = NSEmap2.get(i);        
              NSE2_Update.add(ains);
        
    }
    
}

        if(NSE1_Update.size()>0)
    
          update NSE1_Update;


        if(NSE2_Update.size()>0)
    
            update NSE2_Update;  


        
   }



public static void ChildToParnet (Id ChildId){
     
    For(Account acc : [select id,name, parentid, (Select id,NSE_1__c,NSE_2__c from contacts)from account where id =: ChildId]){
        
        For(Contact c : acc.contacts ){
            
            if(c.NSE_1__c == true)               
                 NSEint1 = NSEint1 +1;
                 NSEmap1.put(acc.id,NSEint1);
                 
            
            if(c.NSE_2__c == true)
                NSEint2 = NSEint2 + 1;                          
                NSEmap2.put(acc.id,NSEint2);              
            
        }
        
        if(acc.parentid != null)            
           childToParnet(acc.parentid);
        
     }
  }
}

Thanks
Sudhir
MaheemSamMaheemSam
Hi Gokulan,

    Need you help very much on the same request can you please help me I have opened a new case for the continuation of the request.

   Please see below link

https://developer.salesforce.com/forums/ForumsMain?id=9060G000000MVRqQAO#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=OPENQUESTIONS&id=9060G0000005SAKQA2

 Your code is working perfect for account hierarchy if it is linear or same if multiple accounts have same account then it is not working can you please suggest me.

Thanks
Sudhir