In several OSC (Oracle Sales Cloud) based application, there comes a requirement where a currency value needs to be formatted according to the currency format of the locale. In general Groovy compiler supports NumberFormat abstract class which can be called as NumberFormat.getCurrencyInstance() which returns the formatted object. This simple method-call will convert the given number to the required currency format. But OSC compiler for groovy does not support the method call to the aforementioned abstract class.
We came across a similar situation, where we had to format one of the fields to US currency format and the formatted value needs to be passed in the email template. To achieve this we implemented a groovy script that converts the given number to string, parses it and converts to the desired format by calculating the length of the given value.
IMPLEMENTATION OF NUMBERFORMAT GROOVY SCRIPT:
- Initially a temporary variable gets the value of the field that needs to be formatted. Assuming the current value is in the string format, the length of the value is calculated based on the decimal.
def tempValue = c_Value //e.g. $USD 9765645.09
def currentIndex = tempValue.indexOf('.')
def currencyValue
if(currentIndex<0)
currencyValue = tempValue
else
{
currencyValue =tempValue.substring(0,currentIndex)
}
def currencyLength = currencyValue.length()
- After 3 digit formatting, the obtained values are added into a list.
def temp=0
def emptyList = []
if(currencyLength%3 !=0)
temp = currencyLength%3
if(temp!=0 && temp<=currencyLength ){
emptyList.add(currencyValue.substring(0,temp))
}
else
{
emptyList.add(currencyValue.substring(0,temp+3))
temp=temp+3
}
while(temp<currencyLength)
{
emptyList.add(currencyValue.substring(temp,temp+3))
temp=temp+3
}
- Finally all the values in the list are concatenated as comma separated string and returned as desired US currency format.
String finalCurrency = ""
int i
for(i=0;i<emptyList.size();i++)
{
finalCurrency+=emptyList[i]+','
}
return "\$"+finalCurrency.substring(0,finalCurrency.length()-1) //e.g. $9,765,645
The above code is defined as a global function formatCurrency which could be referred in the text formula field as below,
def currencyValue = OptyRevenue_c
def tempValue = currencyValue.toString()
def finalCurrency=adf.util.formatCurrency(tempValue)//has the formatted value