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
klabklab 

Use Trigger to Grab Id of Record Just Updated in UI

Hey all,

 

I'm looking for some direction on syntax that will grab the Id of a record that was updated in the UI for further processing in my custom classes. The application is to recreate a roll-up summary field scenario, which will be used as a building block for further complex calculations that are simiar to how roll-up summary fields work but are more complex that what declaritive programming offers.

 

Let's say I have a Parent-Child relationship and want to keep track of an average of the children and store it on the parent object. Instead of using the RSF to compute the average, I would like to use a Trigger to grab the ID of the child record that was just updated in the UI, pass it to a custom Class where the it is used in a query to find the child recrords with the same parent record. The calculation will then take place and the parent record will be updated with the new value.

 

I had no problem doing this using execute anonymous when I hard-code the child record, but I need to have a way to dynamically grab that child record Id to kick off the query and calculations in the class.

 

Child_Object__c myChild = [SELECT c.Parent__c from Child_Object__c c WHERE 'a02F00200055uTU' =:c.Id]

 

Is something like the below allowed in a Trigger to grab the Id? I'm focused on the WHERE clause. Does that ensure that myChild is set to the record that was just updated in the UI?

 

Child_Object__c myChild = [SELECT c.Parent__c from Child_Object__c c WHERE Id =:c.Id]

 

Once I know I have the Id, I can pass that to the class and run the right calculations.  Maybe the Trigger is not the way to do this, but I can't think of another way to call the class. Here is someone else trying to do it as well, http://boards.developerforce.com/t5/Apex-Code-Development/Apex-Rollup-Summary-Trigger-more-than-one-Sum-in-a-map/m-p/357933/highlight/true#M63499.

 

Thank you

Jeff MayJeff May

If you put the trigger on the child object and detect the conditions you care about, you have the ID you need to pass to the 'rollup processor'

klabklab

Hi JeffM,

 

I was able to accomlish this using the isUpdate, isInsert, etc. context variables to run logic on the child records and process it all in the trigger. So the only conditions that were met were those context variables, but I still am not sure if it is possible to ensure it only runs for the field that was just updated in the UI (that Id only), but then again, I don't think that matters, because the trigger fires on any change to a child record, which is needed in roll-up summary fields anyway.

 

Hopefully my reply makes sense to an advanced developer like you, and thank you for replying!

 

K