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
Sufal KanungoSufal Kanungo 

Employee count in the department object in the headcount field through the apex trigger??

I am new in salesforce. How can I get the employee count in the department object in the headcount field through the apex trigger??
Best Answer chosen by Sufal Kanungo
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sufal,

Can you try the below trigger on Employee object.
 
trigger Updatecount on Employee__c (after insert, after update, after delete, after undelete) {

   List<Department__c> ct = new List<Department__c>();
   
   Set<Id> deptids = new Set<Id>();
   
   if(Trigger.isDelete) {
     for(Employee__c test:Trigger.Old) {
      
        deptids.add(test.Department__c);   
    
     }   
   
   }
   else
   if(Trigger.isUpdate) {

     for(Employee__c test:Trigger.New) {
      
        deptids.add(test.Department__c);   
    
     }

     for(Employee__c test:Trigger.Old) {
      
        deptids.add(test.Department__c);   
    
     }   
   
   }
   else
   {
     for(Employee__c test:Trigger.New) {
      
        deptids.add(test.Department__c);   
    
     }
   }
   
   AggregateResult[] groupedResults = [SELECT COUNT(Id), Department__c FROM Employee__c where Department__c IN :deptids GROUP      BY Department__c ];
   
   for(AggregateResult ar:groupedResults) {
     
     Id custid = (ID)ar.get('Department__c');
     
     Integer count = (INTEGER)ar.get('expr0');
     
     Department__c cust1 = new Department__c(Id=custid);
     
     cust1.headCount__c = count;
     
     ct.add(cust1);
      
   }
   
   
   update ct;

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you confirm how are Employee and Department objects related to each other. Which is Parent and which object is Child object.

Thanks,
 
Sufal KanungoSufal Kanungo
Hello Praveen,
The department is the parent object and Employee is the child object Basically, it is the master-detail relationship.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sufal,

Can you try the below trigger on Employee object.
 
trigger Updatecount on Employee__c (after insert, after update, after delete, after undelete) {

   List<Department__c> ct = new List<Department__c>();
   
   Set<Id> deptids = new Set<Id>();
   
   if(Trigger.isDelete) {
     for(Employee__c test:Trigger.Old) {
      
        deptids.add(test.Department__c);   
    
     }   
   
   }
   else
   if(Trigger.isUpdate) {

     for(Employee__c test:Trigger.New) {
      
        deptids.add(test.Department__c);   
    
     }

     for(Employee__c test:Trigger.Old) {
      
        deptids.add(test.Department__c);   
    
     }   
   
   }
   else
   {
     for(Employee__c test:Trigger.New) {
      
        deptids.add(test.Department__c);   
    
     }
   }
   
   AggregateResult[] groupedResults = [SELECT COUNT(Id), Department__c FROM Employee__c where Department__c IN :deptids GROUP      BY Department__c ];
   
   for(AggregateResult ar:groupedResults) {
     
     Id custid = (ID)ar.get('Department__c');
     
     Integer count = (INTEGER)ar.get('expr0');
     
     Department__c cust1 = new Department__c(Id=custid);
     
     cust1.headCount__c = count;
     
     ct.add(cust1);
      
   }
   
   
   update ct;

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Sufal KanungoSufal Kanungo
Sorry, it is not working for me... it is a lookup relationship, I was mistaken.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sufal,

This works for both Master Detail and Lookup relations. Can you confirm what exactly is not working here. Did you trigger got saved without any issue?

Thanks,
 
Sufal KanungoSufal Kanungo
Yes, it got saved without any issues
.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sufal,

Did you try creating a new. Employee record under any of the Deparment ?

Thanks.
 
Sufal KanungoSufal Kanungo
Yes, I did. But nothing is reflected in the headcount field of the department object.
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sufal,

The above code should work with out any issue because it is working as expected in my org.  Are you checking correct department record ( Department related to the Employee you edited) ?

Thanks,
 
Sufal KanungoSufal Kanungo
Yes, Praveen. I have created one department object only.
Sufal KanungoSufal Kanungo
Thank you, Praveen, your code worked perfectly for me. Thanks for helping.