Expressions
Introduction
Expressions change a value based on a function or combination of functions.
Named items (for example, objects, lists, or variables) can be called in an expression by inserting the name of the item and adding a dollar sign (for example, $customer
could refer to an object named customer
). Expressions can also include Mendix system items which contain information about the current user session. See System Items, below.
Attributes and associations of objects are accessed using a slash (for example, the Name attribute of the customer object is referred to as $customer/Name
, and the CRM.Customer_Order association of the customer object is referred to as $customer/CRM.Customer_Order
).
Attributes of associated objects can be accessed using multiple slashes (for example, the Number attribute of a single associated CRM.Order is referred to as $customer/CRM.Customer_Order/CRM.Order/Number
).
You can combine functions in an expression. In this case, you can use brackets to determine the priority and associativity of calculations. For example, the SellingPrice is being calculated based on the default Price and Discount attributes:
$CurrentPrice/Price - (($CurrentPrice/Price **div** 100) * $OrderLine/Discount)
Arithmetic functions (subtraction, dividing, and multiplying) are being combined here.
Examples
For example, you have an object called package with two attributes: weight
(decimal) and shippingCosts
(decimal). If the weight of a package is less than one kilogram, there are no shipping costs. Otherwise, the shipping costs are €5.00. The expression for changing the shippingCosts
attribute is:
if $package/weight < 1.00 then 0.00 else 5.00
false
. The object’s attribute cannot be accessed and the expression cannot be evaluated. This can be crucial when evaluating multiple statements within an expression. See the examples below for more information.
Evaluating the expression:
$emptyObject/attribute != $validObject/attribute or $emptyObject = empty
will always return false
, as long as emptyObject
is empty. The second part of the statement never gets evaluated.
To have both checks evaluated, the order of statements needs to be reversed:
$emptyObject = empty or $emptyObject/attribute != $validObject/attribute
This way the first statement gets evaluated.
Regular Expressions
Regular Expression resource documents cannot be used in expressions. However, the format of regular expressions, sub-expressions, and quantifiers used in regular expression strings is the same as the ones described in the Expression section of Regular Expressions.
System Items
Mendix provides you with a number of system items which describe the current user’s session. You can use these in the same way as any other named item.
$currentUser
This is an object of type System.User
which contains the attributes for the currently signed-in user.
$currentSession
This is an object of type System.Session
which contains the attributes for the current user session.
Unary Expressions
Arithmetic Expressions
Relational Expressions
- Less than (
<
) - Greater than (
>
) - Less than or equal to (
<=
) - Greater than or equal to (
>=
) - Equal to (
=
) - Not equal to (
!=
)
Special Checks
- Checking for an empty object
- Checking for an empty object member
- Checking if an object is new (
isNew
) - Checking if an object is synced (
isSynced
) - Checking if a synchronization is running (
isSyncing
)
Boolean Expressions
If Expressions
if
– performs a conditional action
Mathematical Function Calls
max
– the maximum of a list of numbersmin
– the minimum of a list of numbersround
– the rounding of a floating-point number, optionally to a specified precisionrandom
– random number generationfloor
– the rounding of a floating-point number downceil
– the rounding of a floating-point number uppow
– the exponentiationabs
– the absolute valuesqrt
– the square root
String Function Calls
toLowerCase
– converts the string to lower-casetoUpperCase
– converts the string to upper-casesubstring
– gets a part of a stringfind
– gets a sub-string positionfindLast
– gets the last sub-string positioncontains
– contains the sub-stringstartsWith
– determines whether a string starts with the specified sub-stringendsWith
– determines whether a string ends with the specified sub-stringtrim
– removes the leading and trailing whitespaceisMatch
– matches a regular expressionreplaceAll
– replaces the occurrences of a sub-stringreplaceFirst
– replaces the first occurrence of a sub-string- String concatenation (
+
) – concatenates strings urlEncode
– converts a string to be used in a URLurlDecode
– converts a string back from a URL
Date Creation
dateTime
– creating a date value using the server’s calendardateTimeUTC
– creating a date value using the UTC calendar
Begin-of Date Function Calls
BeginOfDay
– calculates the beginning of the day compared to the initial dateBeginOfWeek
– calculates the beginning of the week compared to the initial dateBeginOfMonth
– calculates the beginning of the month compared to the initial dateBeginOfYear
– calculates the beginning of the year compared to the initial date
End-of Date Function Calls
EndOfDay
– calculates the end of the day compared to the initial dateEndOfWeek
– calculates the end of the week compared to the initial dateEndOfMonth
– calculates the end of the month compared to the initial dateEndOfYear
– calculates the end of the year compared to the initial date
Between Date Function Calls
millisecondsBetween
– the milliseconds between two datessecondsBetween
– the seconds between two datesminutesBetween
– the minutes between two dateshoursBetween
– the hours between two datesdaysBetween
– the days between two datesweeksBetween
– the weeks between two datescalendarMonthsBetween
- the months between two datescalendarYearsBetween
- the years between two dates
Add Date Function Calls
addMilliseconds
– adds milliseconds to a dateaddSeconds
– adds seconds to a dateaddMinutes
– adds minutes to a dateaddHours
– adds hours to a dateaddDays
– adds days to a dateaddDaysUTC
– adds days to a date using the UTC calendaraddWeeks
– adds weeks to a dateaddWeeksUTC
– adds weeks to a date using the UTC calendaraddMonths
– adds months to a dateaddMonthsUTC
– adds months to a date using the UTC calendaraddQuarters
– adds quarters to a dateaddQuartersUTC
– adds quarters to a date using the UTC calendaraddYears
– adds years to a dateaddYearsUTC
– adds years to a date using the UTC calendar
Subtract Date Function Calls
subtractMilliseconds
– subtracts milliseconds from a datesubtractSeconds
– subtracts seconds from a datesubtractMinutes
– subtracts minutes from a datesubtractHours
– subtracts hours from a datesubtractDays
– subtracts days from a datesubtractDaysUTC
– subtracts days from a date using the UTC calendarsubtractWeeks
– subtracts weeks from a datesubtractWeeksUTC
– subtracts weeks from a date using the UTC calendarsubtractMonths
– subtracts months from a datesubtractMonthsUTC
– subtracts months from a date using the UTC calendarsubtractQuarters
– subtracts quarters from a datesubtractQuartersUTC
– subtracts quarters from a date using the UTC calendarsubtractYears
– subtracts years from a datesubtractYearsUTC
– subtracts years from a date using the UTC calendar
Trim to Date
trimToSeconds
– trims to secondstrimToMinutes
– trims to minutestrimToHours
– trims to hourstrimToHoursUTC
– trims to hours using the UTC calendartrimToDays
– trims to daystrimToDaysUTC
– trims to days using the UTC calendartrimToMonths
– trims to monthstrimToMonthsUTC
– trims to months using the UTC calendartrimToYears
– trims to yearstrimToYearsUTC
– trims to years using the UTC calendar
To String
See To String for details.
Length
See Length for details.
Parse Integer
See Parse Integer for details.
Parse and Format Decimal Function Calls
parseDecimal
– converts a string to a decimalformatDecimal
– converts a decimal to a string
Parse and Format Date Function Calls
parseDateTime[UTC]
– converts a string to a date valueformatDateTime[UTC]
– converts a date value to a stringformatTime[UTC]
– converts the time part of a date value to a stringformatDate[UTC]
– converts the date part of a date value to a stringdateTimeToEpoch
– converts a date to a longepochToDateTime
– converts a long to a date
Enumerations in Expressions
getCaption
– gets the caption of an enumeration value in current languagegetKey
– gets the technical name of an enumeration value