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
Z man0Z man0 

Max Date Display on Standard Object

Hi Everyone:

I have 2 objects:
Object 1: Standard Object [Contact]
Object 2: Custom Object [Apple]
Relationship: Look up relationship. 
Apple records can be found under contact related list.

Is it possible to display the latest created date from the related list on the conact custom field "latest date"


 
Best Answer chosen by Z man0
CloudGeekCloudGeek
Hi There,

Try this trigger on Apple__c:
 
trigger updateLatestDate on Apple__c (after insert) 
{ 
  Map<ID, Apple__c> contactTOApple = new Map<ID, Apple__c>(); //Making it a map instead of list for easier lookup
  List<Id> listIds = new List<Id>();

  for (Apple__c childObj : Trigger.new) 
  {
    listIds.add(childObj.Contact__c);
    contactTOApple.put(childObj.Contact__c,childObj);
  }

  List<Contact> cons = [SELECT Id,Latest_Date__c FROM Contact WHERE Id IN: ListIds];

if(!cons.isEmpty() && !contactTOApple.isEmpty())
  for(Contact con: cons)
  {
     con.Latest_Date__c = (DATE)contactTOApple.get(con.Id).CreatedDate.date(); //if you have created Latest Date as DATE
//if it's DATETIME field use this line: con.Latest_Date__c = (DATE)contactTOApple.get(con.Id).CreatedDate;
  }

  update cons;
  
}

 

All Answers

CloudGeekCloudGeek
Hello Z,

If I understand it correctly, Contact has look up to Apple ? 
Then,
A (before insert/) After Insert trigger on Apple__c to update
the Latest_Date__c  on Contact using CreatedDate  from would be more appropriate I believe.
 
Z man0Z man0
Hi Cloud,

Apple has a look up field to Contact. 
CloudGeekCloudGeek
Hi There,

Try this trigger on Apple__c:
 
trigger updateLatestDate on Apple__c (after insert) 
{ 
  Map<ID, Apple__c> contactTOApple = new Map<ID, Apple__c>(); //Making it a map instead of list for easier lookup
  List<Id> listIds = new List<Id>();

  for (Apple__c childObj : Trigger.new) 
  {
    listIds.add(childObj.Contact__c);
    contactTOApple.put(childObj.Contact__c,childObj);
  }

  List<Contact> cons = [SELECT Id,Latest_Date__c FROM Contact WHERE Id IN: ListIds];

if(!cons.isEmpty() && !contactTOApple.isEmpty())
  for(Contact con: cons)
  {
     con.Latest_Date__c = (DATE)contactTOApple.get(con.Id).CreatedDate.date(); //if you have created Latest Date as DATE
//if it's DATETIME field use this line: con.Latest_Date__c = (DATE)contactTOApple.get(con.Id).CreatedDate;
  }

  update cons;
  
}

 
This was selected as the best answer
Z man0Z man0
Cloud,

How do I update the code if need the following requirement: 

A bunch of apple records are associated with a single contact. How will I update the code to display the Max of created date of apple records instead of latest created date?

Example :

Contact A
Apple1 Created Date 05/02/2016 Custom Date 05/01/2016
Apple2 Created Date 05/03/2016 Custom Date 05/14/2016
Apple3 Created Date 05/05/2016 Custom Date 05/10/2016

I need 05/14/2016 to be displayed on the Latest date