+ Start a Discussion

Trigger to Get the latest Created Date on a Parent Object from the child object

Hi , 
I have a trigger to get the fields from Child object to parent object and i was able to succesfullt get all the information i need . Only part i was missing is when i try to get the recently created record created date it gives me the oldest (first created record ) . 
trigger ContactAlerts on ET4AE__IndividualEmailResult__c (after insert, after update) {

Map<ID, Contact> ConToUpdate = new Map<ID, Contact>();   
List<Id> listIds = new List<Id>();
Boolean isExists = false;

list<ET4AE__IndividualEmailResult__c> EmailList = new list<ET4AE__IndividualEmailResult__c>();

for (ET4AE__IndividualEmailResult__c childObj : Trigger.new){

ConToUpdate = new Map<Id, Contact>([SELECT id, ET_Clicked_Atleat_Once__c ,ET_Hard_bounce__c,ET_Unsubscribed__c,(SELECT ID,CreatedDate,ET4AE__DateUnsubscribed__c, ET4AE__HardBounce__c,ET4AE__NumberOfTotalClicks__c  FROM ET4AE__IndividualEmailResults__r) FROM Contact WHERE ID IN :listIds]);

for (Contact Con: ConToUpdate.values()){
isExists = false;
for(ET4AE__IndividualEmailResult__c  IER :Con.ET4AE__IndividualEmailResults__r){
 if(IER.ET4AE__NumberOfTotalClicks__c == 1){
  Con.ET_Clicked_Atleat_Once__c = 1;
  isExists = true;
  if(isExists == false)
  Con.ET_Clicked_Atleat_Once__c = 0;
 if(IER.ET4AE__HardBounce__c == True){
 Con.ET_Hard_bounce__c = true;
  if(IER.ET4AE__DateUnsubscribed__c != null){
 Con.ET_Unsubscribed__c = true;
 if(IER.CreatedDate != null){
 Con.ET_Last_Email_Sent_date__c = IER.CreatedDate;
Chitiz AgarwalChitiz Agarwal
Just put "order by Created Date desc" in your query. That will do.
Hi Kumar,

Thanks for looking at it. I am still having the same old date .