Employee__c can have several Time entries(T_A__C), so I am calculating the total number of days worked by each employee.
T_A__C has lookup to Employee__C, I am using below trigger on Employee__C, I can save it successfully but when I am editing the Employee__C it is showing me below error -
FATAL_ERROR System.SObjectException: Invalid field T_A__c.Employee_on_Job__r.Id for AggregateResult
Trigger:
trigger TestTrigger on Employee__c (after insert,after update) {
List<Employee__c> emplIst = new List<Employee__c>();
List<AggregateResult> ar = [Select Sum(Days_in_number__c) daysSum, T_A__c.Employee_on_Job__r.Id
from T_A__c
Group By T_A__c.Employee_on_Job__r.Id];
Map<id , Double> map1= new Map<id , Double>();
Set<Id> ids = new Set<Id>();
for(AggregateResult result : ar)
{
map1.put((String)result.get('T_A__c.Employee_on_Job__r.Id'), (Double)result.get('daysSum'));
ids.add((ID)result.get('T_A__c.Employee_on_Job__r.Id'));
}
List<Employee__c> o = [Select id, Total_number_of_days_worked__c from Employee__c where id IN : ids];
for(Employee__c opp : o)
{
opp.Total_number_of_days_worked__c = map1.get(opp.id);
emplIst.add(opp);
}
if(emplIst.size()>0)
update emplIst;
}
===============
Employee_on_Job__c
instead ofT_A__c.Employee_on_Job__r.Id
and in soql use group byEmployee_on_Job__c
field