Page tree

This is the documentation for ConfiForms Server/Data Center app

However, this might also work for ConfiForms cloud and in most cases it does. But please see this page to understand the differences between server and cloud versions of the ConfiForms app.


Sometimes, especially when you create a mapping for JIRA issue creation from ConfiForms record you want to make some conditional statements.

Like, not sending the date value when it is not set on ConfiForms side or not send some numeric value which is, again, not set on the ConfiForms side.

To do that you will need to embed some Velocity syntax into your mapping. More about Velocity templates you can read here https://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html

So, in the example below you can see that we do not send the description field if one of the description fields is not set

Important block here is:

#set($desc = "[entry.desc2]")

#if($desc)
   "description": "[entry.desc1] \n [entry.desc2]",
#end

On line #1 we set a value from field "desc2" into a local variable in Velocity

On line #3 we start an IF-END block where we check if the local variable (which we set on line #1) is set (in Velocity, an empty string is "nothing" and evaluates to "false", so when it is empty then it will not go into the IF-END block, and if not empty (and is "something") then it will go into the block and will add the description into the JIRA JSON mapping)

You can check for whatever field you need, but please remember to give unique names for local variables and check for respected variables accordingly

#set($desc = "[entry.desc2]")

Here, the variable is names $desc 


Important note, check the link from apache on Velocity templates, https://wiki.apache.org/velocity/CheckingForNull

Copied below for quicker reference

Q: I want to check for null, something like this:
#if ($car.fuel == null)
A: There are several approaches. Select the one most suitable depending on what you really want to do. (Thanks, everybody, for all the feedback on the user list.) See also: Bugzilla #20999, Bugzilla #27741, VelocityNullSupport.
Approach 1: Use the fact that null is evaluated as a false conditional. (cf. http://velocity.apache.org/engine/devel/user-guide.html#Conditionals)
#if( ! $car.fuel )
Note: The conditional will also pass if the result of $car.fuel is the boolean false. What this approach is actually checking is whether the reference is null or false.


Approach 2: Use the fact that null is evaluated as an empty string in quiet references. (cf. http://velocity.apache.org/engine/devel/user-guide.html#quietreferencenotation)
#if( "$!car.fuel" == "" )
Note: The conditional will also pass if the result of $car.fuel is an empty String. What this approach is actually checking is whether the reference is null or empty.


BTW, just checking for empty can be achieved by:
#if( "$car.fuel" == "" )
Approach 3: Combine Approach 1 and 2. This will check for null and null only.
#if ((! $car.fuel) && ("$!car.fuel" == ""))
Note: The logic underlying here is that: "(null or false) and (null or > empty-string)" => if true, must be null. This is true because "false and empty-string and not null" is never true. IMHO, this makes the template too complicated to read.


Approach 4: Use a Tool that can check for null (NullTool,ViewNullTool).
#if( $null.isNull($car.fuel) )
Note: Of course, NullTool must be in the Context as $null in this case.


Approach 5: Don't check for null directly, use a self-explaining method.
#if( $car.fuelEmpty )
Note: This is my (Shinobu Kawai's) recommended solution. You have to implement the method, but it makes the template so easy-to-read.


public boolean isFuelEmpty()
{
  // return true if fuel is empty.
}
Approach 6: Use a custom directive. cf. IfNullDirective, IfNotNullDirective
#ifnull( $car.fuel )
#ifnotnull( $car.fuel )
Note: You will have to register the directive in your velocity.properties.


userdirective = org.apache.velocity.tools.generic.directive.Ifnull
userdirective = org.apache.velocity.tools.generic.directive.Ifnotnull