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
Marco P.Marco P. 

Add Number sequence afterwards based on this Group and Order.

Hi all,

I need some help with the following. After the collection of different records across Salesfore, a set of new invoice records is created like the example below

This list is Grouped by Invoice number and Ordered by Header


Name:                  Header                 Renevue             Invoice number                
Record1               H                             73                           IN2022/500
Record2               X                             100                        IN2022/500
Record3               X                             -23                         IN2022/500
Record4               H                             144                        IN2022/586
Record5               X                             75                           IN2022/586        
Record6               X                             -5                           IN2022/586
Record7               X                             84                           IN2022/586
Record8               X                             -10                         IN2022/586
Record9               H                             180                        IN2022/673
Record10            X                             65                           IN2022/673        
Record11            X                             -3                           IN2022/673
Record12            X                             45                           IN2022/673
Record13            X                             -15                         IN2022/673
Record13            X                             99                           IN2022/673
Record13            X                             -11                         IN2022/673

I’d like to number this list of records afterwards based on this Group and Order. The result should be like this.

Name:                  Header                 Renevue             Invoicenumber                Number
Record1               H                             73                           IN2022/500                        0
Record2               X                             100                        IN2022/500                        1
Record3               X                             -23                         IN2022/500                        2
Record4               H                             144                        IN2022/586                        0
Record5               X                             75                           IN2022/586                         1
Record6               X                             -5                           IN2022/586                        2
Record7               X                             84                           IN2022/586                        3
Record8               X                             -10                         IN2022/586                        4
Record9               H                             180                        IN2022/673                        0
Record10            X                             65                           IN2022/673                         1
Record11            X                             -3                           IN2022/673                        2
Record12            X                             45                           IN2022/673                        3
Record13            X                             -15                         IN2022/673                        4
Record13            X                             99                           IN2022/673                        5
Record13            X                             -11                         IN2022/673                        6

I am really looking forward to anyone’s reaction.
Thanks in advance !

James (CloudAnswers)James (CloudAnswers)
If you guarantee that the items are always created in order, you could use a trigger or flow to populate the number value, but it will slow down the system a bit (and might make you hit a limit if your org already has many other triggers/flows).  

An alternative is to create an apex batch that sorts the data by the right key (maybe that's name, header, and invoicenumber?) and then use a stateful interface on the job in apex to track and update the "Number" field where necessary.