You need to sign in to do that
Don't have an account?

SOQL - query related person's account- update field - most recent date - Inner join
Hello,
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 most recent appointment date (Max date) per account on a custom field (Most_Recent_Appointment_Date__c) that is built on the account (Persons account) object:
Object #1: (Appointments)
Appointment ID Account ID, Name, Apt Date, Weight....etc.
---------------------------------------------------------------------------------
Apts_001112 001 John 01/01/2017 160
Apts_001114 002 Nicole 11/05/2016 180
Apts_001113 001 John 04/05/2017 175
Apts_001115 003 Mark 05/05/2017 190
Apts_001116 002 Nicole 12/15/2016 200
Object #2: (Accounts)
ID, Name....etc.
--------------------------
001 John
002 Nicole
001 John
003 Mark
002 Nicole
-----------------------------------------------------------------------------------------------------------------------------------------------------
Trigger MostRecentAppointmentDate on Accounts (After update, after insert) {
List <Accounts> AccountsList = new List<Accounts>();
Set<id> AcntsID = new Set<id>();
for(Account a:trigger.new) {
AcntsID.add(a.ID);
}
For (Appointments Apts : Trigger.new) {
Appointments MostRecentApts = [SELECT Scheduled_date__c, Weight__c, Patient__c
FROM Appointments__c
WHERE Patients__c IN:AcntsID
GROUP BY Patients__c
ORDER BY Scheduled_date__c DESC
LIMIT 1];
Set Most_Recent_Appointment_Date__c = MostRecentApts
}
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 most recent appointment date (Max date) per account on a custom field (Most_Recent_Appointment_Date__c) that is built on the account (Persons account) object:
Object #1: (Appointments)
Appointment ID Account ID, Name, Apt Date, Weight....etc.
---------------------------------------------------------------------------------
Apts_001112 001 John 01/01/2017 160
Apts_001114 002 Nicole 11/05/2016 180
Apts_001113 001 John 04/05/2017 175
Apts_001115 003 Mark 05/05/2017 190
Apts_001116 002 Nicole 12/15/2016 200
Object #2: (Accounts)
ID, Name....etc.
--------------------------
001 John
002 Nicole
001 John
003 Mark
002 Nicole
-----------------------------------------------------------------------------------------------------------------------------------------------------
Trigger MostRecentAppointmentDate on Accounts (After update, after insert) {
List <Accounts> AccountsList = new List<Accounts>();
Set<id> AcntsID = new Set<id>();
for(Account a:trigger.new) {
AcntsID.add(a.ID);
}
For (Appointments Apts : Trigger.new) {
Appointments MostRecentApts = [SELECT Scheduled_date__c, Weight__c, Patient__c
FROM Appointments__c
WHERE Patients__c IN:AcntsID
GROUP BY Patients__c
ORDER BY Scheduled_date__c DESC
LIMIT 1];
Set Most_Recent_Appointment_Date__c = MostRecentApts
}
NOTE: This code has not been tested and may contain typographical or logical errors
NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
All Answers
NOTE: This code has not been tested and may contain typographical or logical errors
NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
Thanks a lot for helping me out. I just had a little problem with the last line, I keep getting this error message (Variable does not exist: account at line 36 column 16) and I tried sub it with all these different variables:
AccountName
Account__c
Account__r
Account__r.id
Patients_c
Patient_c
Any clue what could've went wrong?
Also, my next step is to return the most recent weight value in the Most_Recent_Weight__c custom field which is also built on the account object. Can you give me some guidance on how to do so?
Thanks a lot, it works like charm. I used this code as a template to return the inital weight and latest weight value but I'm getting this error message:
Compile Error: Method does not exist or incorrect signature: account.Dr_Chrono_appointments__r.get(Integer) at line 58 column 22
Could you please take a look at it?
Thanks advance!
This will only run once since you have the limit