LUA example: Read and write an additional table

In this example we’ll read and write the data in an additional table. More precisely, we’ll read a quantity multiplier from the form and apply it to every row. Then we’ll update a total, also on the form. The script is applied to the pre-save event, but it can of course be used in other contexts too.

Script Group
Form
Event
Pre-save

First we get the multiplier value from the form with the dataview.getvalue command. In this case the dataview object represents the form; it has already been initialized by the program, so it is ready to use:

moltiplicatore = dataview.getvalue("moltiplicatore")

Now we create the object representing the additional table inside the form, for example fatture_rows, and use it to get the list of rows currently loaded:

tabella = dataview.getatable("fatture_rows")

righe = tabella.getrows()

With the object that gives us access to the rows, we can write a loop that reads them one by one and gets or changes the value of each field. In this example we take the quantity, multiply it and set a second field called quantita_moltiplicata. Then we read it back to add its value to a total

totale = 0

for i=1,tabella.countrows() do

     quantita = righe[i].getvalue("quantita")

     righe[i].setvalue("quantita_moltiplicata",quantita * moltiplicatore)

     totale = totale + righe[i].getvalue("quantita_moltiplicata")

 end

Now that we’ve gone through all the rows, we can set the totale_quantita field on the form.

dataview.setvalue("totale_quantita",totale)