0

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;
}

===============

1
  • try to add Employee_on_Job__c instead of T_A__c.Employee_on_Job__r.Id and in soql use group by Employee_on_Job__c field
    – Ratan Paul
    Commented Jun 13, 2016 at 8:51

2 Answers 2

1

Add an alias in the aggregate query for T_A__c.Employee_on_Job__r.Id as you have already done for Sum(Days_in_number__c) and use the alias in the get:

... Select Sum(Days_in_number__c) daysSum, T_A__c.Employee_on_Job__r.Id jobId
...
map1.put((Id)result.get('jobId'), (Double)result.get('daysSum'));

Aggregate queries generate names such as "expr0" and use those as the names in the AggregateResult if you don't supply an alias so best to use an alias so you control the naming.

0

What you are try to put in map is not field value - but rather path. at the same time methode you are using 'get' for SObject work with fields: the result is this methode try to pick value of field with name 'T_A__c.Employee_on_Job__r.Id' - and there is no such field.

You should either build formula field to store proper value on objet or create proper methode(or logic) to read parent field value.

please check:

sObject acc = [SELECT Owner.Profile.Name FROM Account LIMIT 1];
String profileName = (String)    
acc.getSobject('Owner').getSobject('Profile').get('Name');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .