Issue:
[toc on_off::hide=1]
Your are trying to add calculated fields to a domain. The calculation involves assessment of another field's value based on a condition or set of conditions. Typically (in code), this is done using if/else format, or ternary statements (condition ? 'value if true' : 'value if false'). How can this be achieved in a domain's calculated field? What is the syntax?
Resolution:
Conditions can be evaluated in calculated fields using the "if(condition,value if true,value if false)" function in DomEL. In the following example, I am determining the value of the field by evaluating another field's value (currentsuboppts.Amount). If currentsuboppts.Amount is less than or equal to 5000, the value of my field is the string '<=$5k'. Otherwise, it is '>$5k'.
if(currentsuboppts.Amount <= 5000,'<=$5k','>$5k')
another example using dates:
if(time_by_day1.the_date == d'2013-01-07', null, time_by_day1.the_date)
NOTE: When entering a string value into a calculated field formula, you must use single quotes ('), NOT double quotes (").
Note on Using Groovy
All expressions should be written in the DomEL language. If you want to use Groovy you will need to wrap the code into the groovy() function. But you can't mix DomEL statements and Groovy statements. If so you need to create a separate calculated constant field. Here's an example where we want to show the values of a tenantID and hide the rest. So for that we need to create two fields:
Field1 (tenant) - Gets the tenant ID using groovy, expression:
groovy('authentication.getPrincipal().getTenantId()')
Field2 (anyName) - Based on tenant ID shows one field or another:
if((mt_test.tenant == constant_fields_level.tenantID), mt_test.tenant, mt_test.hidden)
21:24, 16 July 2012 (UTC)
Recommended Comments
There are no comments to display.