X

It's All About the Platform.

Formatting Your Values in Groovy Scripts

Richard Bingham
Senior Development Manager

 

When working with script values there may be occasion when you want the output to include a well-formatter number, such as a price, or a date and time value. This may present a problem because the values are often treated as one type, such as a String for Text Formula fields and therefore any other formatting will not get applied. This often leads to hard to understand values. Fortunately Application Composer groovy allows access to the underlying Formatter class to allow you to convert these specific values in your scripts.

Let's look at a simple example to understand how this works. We add a number field called Estimate Sale to the Lead object. This accepts any number and up to 4 decimal places. As a number field it will be formatted correctly, however if we use the value in a Text Formula field then it will not get the formatting. In the first screenshot below look at Formula1 which is missing the thousands comma as expected by the number format for my English language session. In this illustration a second Text Formula field (Formula2) is added and includes the following code:

def val = EstimateSale_c
def fmt = new Formatter(adf.context.locale)
def ret = (val != null) ? fmt.format('%,.2f', val) : null
return ret

As you can see from this the Formatter object is instantiated and accepts the current user locale (using the adf.context.locale function). This Formatter object is then used to convert the 'val' variable value through its format() method. This accepts some Formatter class specific arguments that define the way the formatting is done. In the example the % means a literal and the second parameter .2f indicates to use two decimals (including rounding).

In the screenshot below the users session language (under Preferences) was set to French. Note the respective number formatting (based on locale) is now used in Formula2:

The Formatter class can also be used to format dates, again based on the locale. Here you can specify day, month, year and time from the original string value and pick out the parts needed. Again this relies on the arguments passed to the format() class.

Finally, the format() method also supports character conversion too, so could be used to pad values to fit a standard, or change values based on a condition. 

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.