Get values from items table[solved]

I had a script that fetched values from items table to sales invoice. It worked in V10 but doesn’t work in V12. It throws

Uncaught (in promise) ReferenceError: so is not defined

Would someone be so kind and check what is wrong? Thanks

frappe.ui.form.on("Sales Invoice", "refresh", function(frm) {
so = []
$.each(frm.doc.items, function(idx, row) {
in_list(so, row.sales_order) ? "": so.push(row.sales_order)});
frm.set_value("sales_order", so.join(", "))});

You may try this.
let so = [];

Thank you! That works.