I am trying to calculate the results of 2 fields. I need to subtract a gross weight value from a scale value. The results should display in a field called net weight. Can this be done with an expression or either a focus event? Below I tried creating a property script on the net weight field but i'm not getting the desired results. What would be the best way to do this?
def valueChanged(self, previousValue, currentValue, origin, missedEvents):
gross_weight = self.parent.parent.parent.parent.getChild("Scrap_Receiving_Flex_Container").custom.gross_weight
scale_weight = self.parent.parent.parent.parent.getChild("Scrap_Receiving_Flex_Container").custom.scale_weight
result = gross_weight - scale_weight
if(gross_weight is not None and scale_weight is not None):
return result
This sort of relative addressing becomes a big headache when moving components in and out of containers. Instead, move gross_weight and scale_weight out to custom properties on the view. Now your code (if you were still using a script) would become,
def valueChanged(self, previousValue, currentValue, origin, missedEvents):
if(view.custom.gross_weight is not None and view.custom.scale_weight is not None):
view.custom.net_weight = view.custom.gross_weight - view.custom.scale_weight
You can use !isNull({path.to.gross.weight.value}) instead.
You should do the null check BEFORE using the values. If one of them really is null, the operation will raise an exception and the code will never reach the null check.
something like this:
gross_weight = self.parent.parent.parent.parent.getChild("Scrap_Receiving_Flex_Container").custom.gross_weight
scale_weight = self.parent.parent.parent.parent.getChild("Scrap_Receiving_Flex_Container").custom.scale_weight
if gross_weight is not None and scale_weight is not None:
return gross_weight - scale_weight
else:
return default_value
# OR
try:
return gross_weight - scale_weight
except TypeError:
return default_value
Yes, put an expression binding on the net weight field's value with that expression:
if (!isNull({view.custom.weight.value}) && !isNull({view.custom.scale.weight}),
{view.custom.weight.value} - {view.custom.weight.value},
default_value
)
Make sure to replace {default_value} by whatever you want it to be. "" or null probably