Custom script to set income account based on tax category

Hi,

I need to set the income account on sales invoices based on the tax category. I want to look at the tax category and set the income account in all the items on the sales invoice. I realise I need to do this with a custom script but I can’t seem to get it to work. I don’t have much experience with javascript but I have done my best to figure it out before posting. Does my code below come close to what is needed?

Maybe someone has already written a script that accomplishes this?

thanks!

frappe.ui.form.on("Sales Invoice", {
    validate: function(frm) {
    
        var items = frm.doc.items;
        var income_account = "8120 - Tax Exempt Sales - CH DE";

        switch (doc.tax_category) { 

            case "Exports": 
				
		        income_account = "8120 - Tax Exempt Sales - CH DE";  // valid income account
	        break;
	        case "Inter EU":

		        income_account = "8125 - Tax-exempt intra-European Union Sales, section 4 no. 1b UStG - CH DE";  // valid income account
            break;
	        default:

		    income_account = "8400 - Sales 19% VAT - CH DE";  
    
        }

    items.forEach(function(entry) {
	    if (entry.income_account !== null) {
		    frappe.model.set_value(entry.doctype, entry.name, "income_account", income_account);
		        }
		    });
}		    

});