Let's stop saving an order
In this example we will see how to prevent saving an order for an insolvent customer. Since it is a very dastric system, wi will divide the porcedure into two scripts.
The first will alert the customer selection, while the second will not allow the order to be saved if the customer or the unpaid condition is not changed.
Script Group
Form
Event
Changed value
As said, the first script will serve to inform the user that the customer selected for the order has a block and this will serve to avoid creating the complete order and only discover at the end thet it is not possible to save it.
if datafield.name() == "gguid_customer" then
t = database.getsql("SELECT insolvent FROM customers WHERE gguid='" .. datafield.getvalue() .. "'")
if t.countrows() > 0 then
rows = t.getrows()
if rows[1].getvalue("insolvent") == 1 then
program.showerror("Attention the customer is insolvent. Could not save order!")
end
end
end
We intercept when the gguid_customer field changes. This is the service field of the sub-table where it stores the gguid of the selection made. It is always better to use the global unique identifier gguid in place of the customer's actual name, this allows you to trace it even if the name is changed.
We read the value of the Insolvent field present in the customers table and display an error message if the value is one. This field will almost certainly be a check on the customer form.
Let's proceed with the second script that will block the saving of the order.
Script Group
Form
Event
Pre saving
t = database.getsql("SELECT insolvent FROM customers WHERE gguid='" .. dataview.getvalue("gguid_customer") .. "'")
if t.countrows() > 0 then
rows = t.getrows()
if rows[1].getvalue("insolvent") == 1 then
errorn.errocode = "E1"
errorn.errormessage ="Attention the customer is insolvent. Could not save order!"
do return end
end
end
As you can see, this is practically identical to the first one, with the exception of the gguid value that will be retrieved from the dataview. The procedure is identical to allow the customer to be able to change the unpaid status. We could in fact open the latter tab and change the value while also keeping the order open form.
The part that changes is the management of the errorn object. Its job is to communicate errors to the program. In this case, it is we who force an error by entering a code, a message and forcing the exit from the script.
Since this is the pre-saving, the program will exit the Save procedure of the card. The error code can be any since it serves us to identify which part of the script generated it.