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
Anthony HallerAnthony Haller 

Get the maximum value of a field from all a parent's child records at new record creation.

Hello,
I can do the following with apex code but I wanted to inquire if there was a way to do it without using formula creators.  I am working on a pair of custom objects (a parent and a child) and I want to apply a sequence field to control the order in which the child object's records would appear in when their parent record was selected.  Creating the field was easy but making it user friendly to create new child records for the parent has proven difficult. 

When a new record is created I originally wanted the sequence field to be default populated by a number that was the highest in the sequence of the parent record's child lots (i.e. if child record sequences 2, 4, and 6 already existed I wanted to the default value to be something like '=MAX(Parent__r.Children__r.Sequence__c) + 2' to and b populated by an 8... and yes I realize that the function syntax I just gave is bogus) .

Not able to find good documentation online for if this was possible to assign as a default value I turned to the Process Builder to have it assigned when a new child record was created via a workflow if the child sequence field was left blank during its creation.  However, once again I could not find good documentation to rule out whether or not I could do this without an Apex Class as I muddled through the process. When I search for Max or Maximum I tend to get posts about the governing limits.

Does anyone know if this might be possible in the formula creator without writing Apex code?  Thanks in advance.

Anthony
kevin lamkevin lam
I just tried this and it worked:
  1. Create a rollup summary field in the parent object to store the maximum sequence number of child records
  2. Create a workflow rule on the child object that is triggered whenever a child record is created
  3. Add a field update to the workflow rule to set Sequence__c to IF(ISBLANK(rollup summary field), 2, rollup summary field  + 2)
Anthony HallerAnthony Haller
Thanks, worked perfectly.