Skip to Main Content

Bredcrumb

Tax Calculator Grid

What to check?

Modify Units, Price, or Tax % to dynamically recalculate the right columns. If Tax % is >= 20 then it will be highlighted in red

How does it work?

The function recalcSums runs on both column changes and grid refreshes.


function calcValues(row) {
	const { UNITS, PRICE, TAX_PCT } = row;

	const subtotal = UNITS * PRICE;
	const tax = (subtotal * TAX_PCT) / 100;
	const total = subtotal + tax;

	return { subtotal, tax, total };
}

function recalcSums(changesArr = undefined) {
	const ucGrid = $("#ucGrid_tax_calc_grid").ucGrid("instance");

	const allRows = ucGrid.getData({ asObjects: true });

    const changes = [];
    
	let i = 0;
	for (const row of allRows) {
		const { subtotal, tax, total } = calcValues(row);
        
        if (row.TAX_PCT >= 20) {
            ucGrid.addCssClass(i, "TAX_PCT", "u-danger-bg");
        } else {
            ucGrid.removeCssClass(i, "TAX_PCT", "u-danger-bg");
        }

		changes.push([i, "SUBTOTAL", subtotal]);
		changes.push([i, "TAX", tax]);
		changes.push([i, "TOTAL", total]);

		i++;
	}
    
    apex.debug.info('changes', changes)
    ucGrid.setValue(changes);
}