One method is to create a file (or data area?) containing totals at the same time you create the invoices. Or a file containing invoice number, customer number and amount. Then your cl program can use the same search criteria on the spool file and the total file.
Creating and maintaining another file can be more trouble than you want to put up with, but sometimes it’s the simplest way.
Or maybe you have an existing open A/R file that you can use to get the data, using the same selection criteria as for the spool files.