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
Mahmoud Coudsi 1Mahmoud Coudsi 1 

SOQL - query related to person's account- update field trigger is saved but not working as expected

Context:

I'm new to Apex and SOQL. I have two objects; Accounts (Master object) and Appointments (Child object) and I'm trying to write a trigger (With SOQL query) that returns the inital weight value (corresponding to the inital date) per account and another trigger that return the latest weight value (corresponding to the most recent appointment) per account on the custom fields inital_weight__c and latest_weight__c respectively. Both fields are built on the account (Persons account) object:

Problem:

While the trigger for the latest weight (GetLatestWeightValue) works like a charm, the other trigger for the initial weight value (GetInitialWeightValue)​ is not working for some reason.

I attached both trigger's codes below, I'd really appreciate it if someone can take a look at the codes and point out what wrong with the 1st trigger; GetInitialWeightValue

GetInitialWeightValue Trigger:  returns the inital weight value (corresponding to the inital date) per account
Trigger GetInitialWeightValue on Appointments__c(after insert, after update) {
    Set<Id> accountIds = new Set<Id>();

    for (Appointments__c appt : trigger.new) {
        accountIds.add(appt.Patient__c);
    }

    //Elimnate the the accounts that don't have IDs for
    accountIds.remove(null);
    
//SOQL query that returns that initial weight value 
    if (!accountIds.isEmpty()) {
        List<Account> accountsToUpdate = new List<Account>(); 
        for (Account account : [
            Select Id,
                (
                    Select Scheduled_time__c, weight__c
                    From Dr_Chrono_appointments__r
                    Order by Scheduled_time__c ASC
                    Limit 1
                )
            From Account
            Where Id in :accountIds
        ]) {
           
            //Declare a decimal variable to store initial weight value 
            Decimal IW = NULL;
            
            // Get(0) to return the first element in the list value
            if (!account.Dr_Chrono_appointments__r.isEmpty()) {
                IW = account.Dr_Chrono_appointments__r.get(0).weight__c;
            }

            accountsToUpdate.add(new Account(
                Id = account.Id,
                initial_Weight__c = IW
            ));
            
        }   
        
        update accountsToUpdate;
        
        }
      }
GetLatestWeightValue​ Trigger:  returns the latest weight value (corresponding to the inital date) per account
Trigger GetLatestWeightValue on Appointments__c(after insert, after update) {
    Set<Id> accountIds = new Set<Id>();

    for (Appointments__c appt : trigger.new) {
        accountIds.add(appt.Patient__c);
    }

    //Elimnate the the accounts that don't have IDs for
    accountIds.remove(null);

    //SOQL query that returns that latest weight value 
    if (!accountIds.isEmpty()) {
        List<Account> accountsToUpdate = new List<Account>();
        for (Account account : [
            Select Id,
                (
                    Select Scheduled_time__c, weight__c
                    From Dr_Chrono_appointments__r
                    Order by Scheduled_time__c desc
                    Limit 1
                )
            From Account
            Where Id in :accountIds
        ]) {
           
            //Declare a decimal variable to store latest weight value 
            Decimal LW = NULL;
            
            // Get(0) to return the first element in the list value
            if (!account.Dr_Chrono_appointments__r.isEmpty()) {
                LW = account.Dr_Chrono_appointments__r.get(0).weight__c;
            }

            accountsToUpdate.add(new Account(
                Id = account.Id,
                Latest_weight__c = LW
            ));
                         
        }   
        
        update accountsToUpdate;
        
        }
      }