In this example we’ll see how a few lines of code can quickly update the data in our database.
Script Group
Program
Event
Action on Program
As you know, tables are linked to each other through the special Subtable field, which lets you select a row of values from a table other than the one handled by the current form. Choosing that row then fills in the desired fields automatically. Since this is a copy of the data and not a link, if you change, say, the customer’s name in their form, the old name remains in the documents where it was used. With this script we can realign the names in the documents.
To use this script, set it up as an action on the program, so you can run it directly whenever you want.
tabella = database.getsql("SELECT * FROM nominativi")
nrighe = tabella.countrows()
righe = tabella.getrows()
First we load the nominativi table, which holds our customers, and get its rows and their number.
for i = 1,nrighe do
gguid = righe[i].getvalue("gguid")
nome = righe[i].getvalue("nominativo")
database.setsql("UPDATE fatture set cliente='" .. nome .. "' WHERE gguid_nominativo='" .. gguid .. "'")
end
In this loop we get the gguid and the name of each contact and write the name back into the invoices that use it, so that it is updated.
This approach is certainly very fast, but it has a few problems.
The first is that, with the SQL query written this way, a customer name containing a single quote would cause an error. To handle it, remember that you just need to double the quotes or use the convap function, as in this example:
"update fatture set cliente='" .. utility.convap(nome) .. "' where ...."
The second problem is that, with a cloud database, the changes would not be synchronized. Synchronization actually relies on other values and functions.
Even if you wanted to synchronize, this approach would still be wrong, because after updating the data it would no longer be possible to tell which records were changed. That would mean sending all the customers to the server again, even though none of them changed.
If you need to synchronize the data, there are always two steps to take in your scripts.
database.setsql("UPDATE fatture SET tid=" .. tostring(utility.tid()) .. " WHERE gguid_nominativo='" .. gnominativo .. "'")
First, update the tid with the current date, because it is the timestamp of the data row.
During synchronization, the program checks which rows have been updated since the last time and sends only those.
tabella = database.getsql("SELECT * FROM fatture WHERE gguid_nominativo=' " .. gnominativo .. "'")
nrighe = tabella.countrows()
righe = tabella.getrows()
for i = 1,nrighe do
database.addsyncbox("fatture",righe[i].getvalue("gguid"))
end
The second step is actually only needed if the script must also run on mobile devices. These use a synchronization system that classifies the data to send in a slightly different way.
In this part of the script we retrieve all the invoice rows used by our customer and mark them to be sent at synchronization with the addsyncbox command.
One last note: it is always good practice to add “and eli=0” as a filter, so you don’t retrieve data that is currently in the trash.
