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
LoveLaceLoveLace 

Update a parent field once a child is inserted or updated - Custom Object

Hello there,

I have two custom object (Employee__c[Credit__c field] and Time_Off__c{Days__c field]) with a master detail relationship (Employee__c the parent , Time_Off__c the child) once o time off is added with a number of days (Days__c) this number of days must be retreived on the employee credit (Credit__c)

Here is the apex classe I developped in order to call it in a trigger before insert/update


List<Employee__c> emp =  [SELECT Name,  (SELECT Name FROM Times_Off__r) FROM Employee__c ];
List<Employee__c> upEmpl = new List<Employee__c>();

List<Time_Off__c> TO = new List<Time_Off__c>();

public void MAJSolde(){
//List<Employee__c> empl = [SELECT Credit__c FROM Employee__c WHERE ID := Id];

for (Employee__c em: emp){
for (Time_Off__c toof: Employee__c.Times_Off__r){
    if(em.Credit__c == 0)
    System.debug('No credit left');
}
else
    em.Credit__c -= em.Times_Off__r.Days__c; 
   
}
}

Where is the probleme
Ramu_SFDCRamu_SFDC
Hi, what is the problem you are seeing? If you are getting any error please provide the details of the error.
LoveLaceLoveLace
 Here's the error Variable does not exist: Employee__c.Times_Off__r obviously the relationship Times_Off__r is unknown although it exist