Example - Check The Lookups Value
Do you need to check while saving that the selected value in the lookup corresponds to the defined condition and notify the user in the pop-up window? Let's have a look at it!
The required script is the following. You place an order and while selecting goods from the lookup, you need to check that the value of the selected items is less than $500.
Firstly, it is necessary to ensure the item Amount for goods would appear in the lookup. The way to achieve it is that in the source table for the lookup in the developer section you select the option "Include in lookup selected value" for the items (in our case - Price). We also recommend naming the item internally (e.g. price). It will be necessary for the following steps.
Now we can set JavaScript in the advanced settings in the table of orders. We will use "Before model save" because we want that the check-up will happen just before saving and not after each change in the scheme.
doo.model.items.value
is the lookup with the items of the goods whose price we want to control.
doo.model.items.value.price
is already the concrete value of the lookup, which we had already set in the advanced settings on the source table of the lookup.
doo.model.items.value.price > 500
is the condition under which we want to notify the user.
We display pop-up window: doo.alert.showWarning('Title','The value of the selected goods must be less than $500')
doo.model.isValid = false
condition is not met (the value of the good is greater than 500) and the record will not be saved. Otherwise, when the condition is met, the record will be saved doo.model.isValid = true
.
As the result is the display of the warning, when the user selects an item from the lookup when the price is greater than $500.
Whole JavaScript is here:
if (doo.model.items.value && doo.model.items.value.price > 500){
doo.alert.showWarning('Title','The value of the selected goods must be less than $500');
doo.model.isValid = false;
} else
doo.model.isValid = true;