Aggregate from one collection, insert to another - with multiple groupings

I’m having trouble with
Aggregating points based on 2 different groupings, then applying those totals to another collection

Working in
Dev Mode

What I’m trying to do
A horse is assigned a division to collect points in. They may also have another division to accumulate points in. I would like to collect the points gathered in multiple classes (database 2026 High Points) and update the horse’s records for the “First Division” and “Second Division” (database Horse Reference).

What I’ve tried so far
I have done all the Googles. I’m not sure if it is better to put this through an automation (not sure how to make that work properly) or if I should have it on an “after Update” for 2026 High Points. I already have something in the 2026 High Points after update and I’m not sure how to make sure that doesn’t get messed up also. I’ve tried this in Sandbox and live. I’ve tried with a button trigger, and I’ve tried as an “after Update”.

Screenshots of Database Set-Up First, Then Code (If I Use A Button)

Database 2026 High Points: “_id”, “recordId”, “horseId”, “division”, “showName”, “className”, “championshipStake”, “placing”, “pointsEarned”

Screenshot 2026-04-10 175431

Database Horse Reference: (_id, not shown), “recordId”, “horseName”, “firstDivision”, “firstDivisionPoints”, “secondDivision”, “secondDivisionPoints”

export async function syncAggregatedData() {
	// 1. Get aggregate results (e.g., sum of points per recordID and division)
	const sumAgg = await wixData.aggregate("2026highpoints")
        .group("recordId", "division")
        .sum("pointsEarned")
        .run();
    const aggresults = sumAgg.items; // Array of {recordId, division, pointsEarnedSum}

	// 2. Query target collection for matching items
    const horserefs = await wixData.query("HorseReference")
        .include("firstDivision", "secondDivision")
        .find();
	
	// 3. Match and prepare items for update
	const itemsToSave = horserefs.items.map(horseinfo => {
		// Find matching aggregate for the horse
		const matchdivone = aggresults.find(agg =>
			agg.recordId === horseinfo.recordId &&
			agg.division === horseinfo.firstDivision);
		const matchdivtwo = aggresults.find(agg =>
			agg.recordId === horseinfo.recordId &&
			agg.division === horseinfo.secondDivision);
		
		if (matchdivone || matchdivtwo) {
			horseinfo.firstDivisionPoints = matchdivone.pointsEarnedSum;
			horseinfo.secondDivisionPoints = matchdivtwo.pointsEarnedSum;
			return horseinfo;
		}
		return null;
	}).filter(item => item !== null);

	// 4. Bulk save
	if (itemsToSave.length > 0) {
		return wixData.bulkSave("HorseReference", itemsToSave);
	}
}

This is something I have less experience with – and again, I’m doing this in my spare time for a not-for-profit. Can’t be paying someone to do it for me, unfortunately. (If needed, the database set-up being referenced for “divisions” will be in the comments.)

Screenshot 2026-04-10 175507

Database High Point Divisions: “_id”, “title”, “divisionName”, “divisionCategory”

On my actual account not the account owner’s…. anyone have any ideas? I’m sure it is something silly that isn’t working.