Strategy Core: Custom Conditions (Advanced)
Please note that you'll need JavaScript skills to fully understand this article.
Context
There are several errors happening in the Strategy Domain because of the error: Cannot read property 'toString' of undefined.
Issue
In the advanced mode in Strategy Core, there are expressions that are not protected against undefined and null. This is especially important when using meta fields in the expression, for example, the custom expression:
(meta.DCA_warning_status.toString() === "SENT" || meta.DCA_warning_status.toString() === "LATE_RESOLVED")
This expression has multiple improvements:
-
If the field is expected to be a string, the use of
.toString()is not required -
If we still want to use
.toString()(even if it is not required), we need to protect it against that field missing in the evaluation context.
Solutions
Simplify Expression
This is the simplest solution:
meta?.DCA_warning_status === "SENT" || meta?.DCA_warning_status === "LATE_RESOLVED"
- It doesn’t require the parenthesis
- It doesn’t require the .toString()
Complex Expression
If we still want the parenthesis and the .toString() we can use:
(meta?.DCA_warning_status?.toString() === "SENT" || meta?.DCA_warning_status?.toString() === "LATE_RESOLVED")
- We keep the parenthesis (not needed in this expression).
- We keep the .toString() but because of the previous question mark, we won’t break if that meta field is not present.
FAQ's
Why keeping the .toString() is valid?
In the previous expression, it is not needed, and it adds complexity. Let’s use the first option, but if you still want it, then protect it against the absence of that field.
In other Strategy Steps you use the .toString(), why shall we avoid it?
The other steps are using that trick to avoid complexity in the autogenerated code, but if you are crafting the code by hand, you can avoid it.
What does the question mark do in the expressions?
It makes sure that the next element in the chain doesn’t break if the value is null or undefined (including that .toString()), without making the expression complex.