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
crob1212crob1212 

Help with Trigger to populate list of Dates

Hi All,

I am new to Apex programming and appreciate any help you can offer.  I have a parent object (Subject__c) which has children (Conditions__c).  The child object has a field called Start_Date__c, which is a Date field.  I want a trigger that will go get the start  date for each Condition and put them in a field on the Subject called Date_s_Of_Injury__c, with each value separated by a semicolon. So, for example, if there were three child records with the Start Dates of 07/01/2001, 10/12/2002 and 12/12/2007, the field on the parent object would look something like this:  Date(s) Of Injury:  07/01/2001; 10/12/2002; 12/12/2007.   I have figured out the code to update the field on the parent if there is only one Start Date.  But I don't know how to go grab multiple start dates.  Here is the code that will add singular dates of service:

trigger UpdateDateOfInjury on Condition__c (after insert) 

{
 
  List<id> subIds = new List<id>();
 
  for(Condition__c cd: Trigger.new)
  { 
    if (cd.Start_Date_Alleged__c != NULL)  
    {
      subIds.add(cd.Subject__c);
    }
        
    system.debug('Check Ids added:' + subIds.size());

   
    List<Subject__c> subjects = [SELECT Id FROM Subject__c WHERE Id IN :subIds];

    system.debug('Check How Many Subjects retrieved:' + subjects.size());
 
    for (Subject__c s: subjects) 
    {
      s.Date_s_of_Injury__c = s.Date_s_of_Injury__c = cd.Start_Date_Alleged__c ;
    }
    
    update subjects;
  }  

}

 

 

By the way, the field on the parent object is a date field.  I tried to make it a text field but got an error b/c I couldn't make a date populate in a text field.

 

Any ideas?

 

Thanks a million. 

SPDSPD

try making the datatype of date in parent as String and then populate the dates from chlid into it using string.valueOf() function...i hope it works though im not sure about this .......

 

 

for (Subject__c s: subjects) 
    {
       s.Date_s_of_Injury__c = string.valueOf(cd.Start_Date_Alleged__c );

    }
   

 

Victor19Victor19
Hi,

I am working on a similar requirement. Did you get a solution for your code? If so.. could you please share your updated code.

Thanks!
Vic