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
mark-azmark-az 

Flag most recent record within the current object?

Hello.  I thought I could pull values up to a parent record using a combination of rollups and workflow, but with the order of operations, the rollup happens after my workflow fires, so my totals are one update behind.  To fix this, I thought I could write a trigger that would identify the most recent child record from that object itself.  Just not sure I can even do this. 

 

I wrote the following simple trigger, but nothing is happening.  I am trying to debug now, but could you please let me know if something like this would even be possible?  Thanks in advance.

 

trigger SetMostRecent on Risk_Assessment__c (after insert, after update) {

integer count=1;
String yes='Y';
String no='N';

  for (Risk_Assessment__c n: Trigger.new) {
      String srmtool = n.SRM_Tools__c;  
      // gets ALL assessments for the parent id of the current record
      List<Risk_Assessment__c> listRAs = [Select Id, Most_Recent_Risk_Profile__c from Risk_Assessment__c
                            where SRM_Tools__c=:srmtool order by CreatedDate desc];
                          
      for(Risk_Assessment__c ra : listRAs)
      { 
        if(count==1){
            ra.Most_Recent_Risk_Profile__c=yes;
            update ra;
        }else{
            ra.Most_Recent_Risk_Profile__c=no;
            update ra;
            }   
       
        count++; 
       
      }
  }                          
} //end trigger

Best Answer chosen by Admin (Salesforce Developers) 
mark-azmark-az

Actually, after digging around in the boards for a few days, I found this excellent post which covers what I was looking to do:

 

http://boards.developerforce.com/t5/Apex-Code-Development/Trigger-to-Update-Status/td-p/264273/highlight/true/page/2

 

I have tested this out, and by doing it this way (on after insert, update, or delete), it fires before sf does the rollups, and allows the rollups to work correctly the first time.  The above post is very detailed and helps explain what they did. 

All Answers

AmitSahuAmitSahu

This is possible using trigger.  You need to create a trigger on the child object after insert and after update and after delete.

Once you do any of these operation just add or subtract the value tp or from the parent record.

mark-azmark-az

Thanks for the info, that makes sense.  It sounds like I would just replace the rollups with the trigger itself.  I was hoping I could just set a 'primary' flag on the child record itself, then let salesforce do the rollups using this flag as a filter.  Do you know if that would be possible?

AmitSahuAmitSahu

No that way automatically salesforce will not roll up them.

mark-azmark-az

Actually, after digging around in the boards for a few days, I found this excellent post which covers what I was looking to do:

 

http://boards.developerforce.com/t5/Apex-Code-Development/Trigger-to-Update-Status/td-p/264273/highlight/true/page/2

 

I have tested this out, and by doing it this way (on after insert, update, or delete), it fires before sf does the rollups, and allows the rollups to work correctly the first time.  The above post is very detailed and helps explain what they did. 

This was selected as the best answer