+ Start a Discussion
Todd B.Todd B. 

I know there is a better way to do this.

I wrote this piece of code to select the Max value from a series of Child records for a Parent reocrd that in not in a Master Detail.  Basically the Parent Record SD_Member__c can have enerous Child records - SD_Mbr_Enroll_Cycles__c.  So I want to capture the Max Enrollment_Status_Date__c and update a field on the Parent Record.  

I wrote the following code and it works but I know there is a better way to write this.  
trigger Update_Latest_Enrollment_Date on SD_Mbr_Enroll_Cycles__c (after insert, after update) {
  Developer     : Todd Barry
  Created       :
  Last Modified : 02/26/2014
  Test Class    : Test_Last_Enrollment_Logic
  Objective     : Update the corresponding Enrollment Date (Latest) - OnTrak Field on the SD Member record.
For (SD_Mbr_Enroll_Cycles__c EC : Trigger.new){

String strId = ec.SD_Member_Stakeholders__c;

List <SD_Mbr_Enroll_Cycles__c> ec1 = [select Enrollment_Status__c, Enrollment_Status_Date__c from SD_Mbr_Enroll_Cycles__c
                                       Where SD_Member_Stakeholders__c =: Strid
                                       And Use_Me__c = True
                                       and Enrollment_Status__c = 'Enrolled'
                                       ORDER BY Enrollment_Status_Date__c DESC LIMIT 1];

Map<string,Date> MaxDate = New Map<string,Date>{};
For (SD_Mbr_Enroll_Cycles__c ec2: ec1)
MaxDate.put(ec2.Enrollment_Status__c, ec2.Enrollment_Status_Date__c) ;                                     
// Not sure I need this line.
//For (SD_Member__c sd : [Select id,Enrollment_Date_Latest_OnTrak__c From SD_Member__c Where id =: strId]){

Sd_Member__C sd1 = new Sd_member__c (id = strId,
          Enrollment_Date_Latest_OnTrak__c = maxdate.get('Enrolled'));

Update sd1;        
You are right when you say there is a better way to write this trigger.  There's a bunch of things here that need improving.  I'd start with this article and go from there:


A couple of pointers:
  1. Move the soql and update dml outside of the for loop.  Having these inside will cause you to reach governor limits if ever inserting/updating in bulk
  2. Use Id instead of String to hold ec.SD_Member_Stakeholders__c
  3. It looks like you are trying to get the latest enrollment status date for the given stakeholder on the SD_Mbr_Enroll_Cycles record.  To start,
    1. Create a list of all Member Stakeholders
    2. Query using that list: Where SD_Member_Stakeholders__c in :yourListOfStakeholders
    3. Create list of SD_Member__c records with what you get back from your query
    4. Update the list
Todd B.Todd B.
Thx Willard.  I did a bunch of research over the weekend and this what I came up with:

trigger Update_Latest_Enrollment_Date_v02 on SD_Mbr_Enroll_Cycles__c (after insert, after update) {

Map<id,SD_Member__C> SDM = New Map<id,SD_Member__C>();
List<id> sdId = New List<id>();

For (SD_Mbr_Enroll_Cycles__c sdec : Trigger.new) {


SDM =  New Map<id,SD_Member__C>([Select Id, Name, Enrollment_Date_Latest_OnTrak__c,
                                (select SD_Member_Stakeholders__c, Enrollment_Status__c, Enrollment_Status_Date__c
                                          From SD_Mbr_Enroll_Cycles__r
                                          Where Use_Me__c = True
                                          and Enrollment_Status__c = 'Enrolled'
                                          ORDER BY Enrollment_Status_Date__c DESC LIMIT 1)
                                From  SD_Member__c
                                Where ID IN : sdid ]);
For(SD_Mbr_Enroll_Cycles__c EC: Trigger.new){
    SD_member__c sd1 = sdm.get(ec.SD_Member_Stakeholders__c);
    sd1.Enrollment_Date_Latest_OnTrak__c = ec.Enrollment_Status_Date__c;

Update sdm.values();


Is this better than before, i.e., more efficient?  I still don't know what half of it means!