Help

Re: Java Automation

736 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Ryan_Mizak
4 - Data Explorer
4 - Data Explorer

I am trying to get a sum total of a column in an automation that will send out an email showing the new total every time an amount is added. I was tasked to create an automation running a script, but I am very new to script writing. Any help would be most appreciated

1 Reply 1

This mornings coffee break, I created this example using a Scripting Extension - once you have an understanding of this, we can then talk about how to gear it within an Automation Script (which will be a very similar exercise, with minor differences).

The key to this is using the Javascript reduce() method. I've also demonstrated how you can filter the data using a filter() method, before the data is summed via reduce().

const emailTable = base.getTable("Numbers Table");

let records = await emailTable.selectRecordsAsync({
	fields: ["Fruit", "Total Mass"]
});

console.info(records);

let initialValue = 0;
let sumMass = records.records
	.filter(
		(record) =>
			record.name !== "Green Apple"
	)
	.reduce((accumulator, record) => record.getCellValue("Total Mass") + accumulator, initialValue);

console.log(sumMass);

Here, we can see the data that's been filtered, then reduced - and the final total;

Karlstens_0-1670886681825.png