LUA example: Update the bank account from the payment due date to the invoice

In this example we’ll see how to update the bank account shown on the invoice, starting from the payment due date.

Script Group
Form
Event
Post-save
gguidp = dataview.getvalue("gguidp")

tap = dataview.getvalue("tap")
if gguidp == "" or tap == "" then
   do return end
end

First we check whether the due date actually has a parent record by checking the gguidp and tap fields, since the whole program links records using these two values.

The gguidp value identifies the parent row, while tap identifies the parent table. If these fields are empty, the due date was created by the user; otherwise it was created by another table.

gguidc = dataview.getvalue("gguid_contoappoggio")

if gguidc == "" then
   do return end
end

We also get the gguid of the bank account. Here too, if there is no value, we exit the script.


tabellaconto = database.getsql("SELECT * FROM contiazienda WHERE gguid='" .. gguidc .. "'")

if tabellaconto.countrows() == 0 then

do return end

end

righe = tabellaconto.getrows()

  

output.print(righe[1].getvalue("nomeconto"))

output.print(righe[1].getvalue("contocorrente"))

output.print(righe[1].getvalue("iban"))

--Get the bank account values from its gguid. These are the values I'll write into the invoice.

database.setsql("UPDATE " .. tap .. " SET TID=" .. tostring(utility.tid()) .. ",contoappoggio='" .. utility.convap(righe[1].getvalue("nomeconto")) .. "',contocorrenteditta='" .. utility.convap(righe[1].getvalue("contocorrente")) .. "',ibanditta='" .. utility.convap(righe[1].getvalue("iban")) .. "' WHERE gguid='" .. gguidp .. "'")

 

database.addsyncbox(tap,gguidp)

program.refreshsection("fatture")

Finally, I update the linked invoice with the account data. If the database is a cloud database, always remember to update the tid (the date and time the record was changed) and to add the record to the syncbox.

Once the data has changed, we force a refresh of the sections that use the invoices table.