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

Help Writing a "Most Recent" Record Trigger
I have a parent object: Cycle__c
The child object is: Sessions__c
Each Cycle__c has multiple sessions. I have put a lookup field, Latest_Session__c, that I want populated with the most recently created Sessions__c record.
I figured I needed a way to denote which Sessions__c record was the most recent, so I put a checkbox on the Sessions__c called Most_Recent_Record__c which is checked by default, and wrote a trigger that unchecks that field for all other Sessions__c records related to the same Cycle__c.
I then tried to write a trigger that populated the Latest_Session__c field on the Cycle__c object, but it seemed to conflict with the other trigger.
Thoughts?? There is a date field on the Sessions__c object, so if that helps and means that I don't need the Most recent checkbox that is fine.
Here is the trigger for unchecking "most recent record" on the Sessions__c object:
trigger clearmostrecentflag on Sessions__c (before insert) { //create a list to hold the records to update List<Sessions__c> oldrecords = new List<Sessions__c>(); for(Sessions__c myrecord: trigger.new){ String parentname = myrecord.Cycle__c; String myrecordid = myrecord.Id; for (Sessions__c myrecordlist : [select Cycle__c from Sessions__c where Cycle__c =:parentname AND Id != :myrecordid AND Most_Recent_Session2__c = true]){ myrecordlist.Most_Recent_Session2__c = false; oldrecords.add(myrecordlist); } } if(oldrecords.size()>0) { update oldrecords; } else{ //do nothing } }
Here is the trigger I attempted to write, to update the blank Latest_Session__c lookup field on the Cycle__c object (essentially, I want that field populated with whichever child Sessions__c record has Most_Recent_Record__c checked as true).
trigger updateLatestSession on Sessions__c (after update) { Set<Id> CycIds = new Set<Id>(); for (Sessions__c sess : Trigger.new) if(sess.Most_Recent_Session2__c != false){ CycIds.add(sess.Cycle__c); } Map<Id, Cycle__c> cycMap = new Map<Id, Cycle__c>( [SELECT Id FROM Cycle__c WHERE Id IN :CycIds]); for (Sessions__c sess : Trigger.new){ cycMap.get(sess.Cycle__c).Latest_Session__c = sess.ID; //create list of ParentObjects List<Cycle__c> ParentObjectList = cycMap.Values(); //save the ParentObjects update ParentObjectList; } }
The error message I got when trying to create a new Sessions__c:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger clearmostrecentflag caused an unexpected exception, contact your administrator: clearmostrecentflag: execution of BeforeInsert caused by: System.DmlException: Update failed. First exception on row 0 with id a0DQ00000044zThMAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateLatestSession: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.updateLatestSession: line 12, column 1: []: Trigger.clearmostrecentflag: line 19, column 1
Any help would be much appreciated.
Thanks!
try this.
All Answers
I see few things you are not doing right in the above class like the query inside a loop and have to change it.
Try these two classes and let me know if it doesnt work.
Thank you for your help! I wound up not getting an error, but the Latest_Session__c field on Cycle__c did not update with the most recent Session created.
I'm wondering if it has to do with this section:
Would I be trying to map the Cycle ID to the Cycle__c field on the Session__c object? Using this language is always trial and error for me. What do you think?
Thanks again! I really appreciate it!
Probably you might need to have after insert trigger to update the cycle__c record with the latest_Session__c Id.
I got this error:
Error: Compile Error: Duplicate variable: cycleList at line 16 column 26
which is the
try this.
THANK YOU!!!!! It worked! I appreciate you helping me work through this!