Custom Function
A custom function in the 200 OK application allows users to define their own logic using the interface provided within the Apex environment. This empowers users to tailor the application to their specific needs, incorporating custom business logics, and data processing operations seamlessly into their processes. Below is the sample class for creating your own Custom Function Apex Class: global class CustomDateTimeFormatter implements lwapic.IICCallableService {
global Object call(String action, Map<String, Object> params) {
Object dataToReturn = new Map<String, Object>();
if (action.equalsIgnoreCase('fx_formatDateTime')) {
dataToReturn = formatDateTime(params);
}
return dataToReturn;
} private Object formatDateTime(Map<String, Object> params) {
List<Object> paramList = (List<Object>) params.get('input');
String dateTimeString = String.valueOf(paramList.get(0));
Integer year = Integer.valueOf(dateTimeString.substring(0, 4));
Integer month = Integer.valueOf(dateTimeString.substring(5, 7));
Integer day = Integer.valueOf(dateTimeString.substring(8, 10));
Integer hour = Integer.valueOf(dateTimeString.substring(11, 13));
Integer minute = Integer.valueOf(dateTimeString.substring(14, 16));
Integer second = Integer.valueOf(dateTimeString.substring(17, 19));
Datetime formattedDateTime = Datetime.newInstanceGmt(year, month, day, hour, minute, second);
Map<String, Object> resultMap = new Map<String, Object>();
resultMap.put('result', formattedDateTime);
return resultMap;
}
} *Note :- 1. When implementing a custom class using this interface, we must use a global access modifier. The lwapic.IICCallableService interface includes a method named call, which accepts two inputs: "string" and "map"(containing a string and an object). 2. In the CustomDateTimeFormatter class, we implement the lwapic.IICCallableService interface. 3. The call method checks the action parameter to determine the specific action to perform. If the action is fx_formatDateTime, it calls the formatDateTime method. 4. This method extracts a datetime string from the input map, parses it, and constructs a datetime object. The formatted datetime is then returned in a map with the key 'result'.
Test Class: @isTest
private class CustomDateTimeFormatterTest {
@isTest
static void testCustomDateTime() {
CustomDateTimeFormatter formatter = new CustomDateTimeFormatter();
String action = 'fx_formatDateTime';
Map<String, Object> params = new Map<String, Object>();
List<Object> inputParams = new List<Object>{'2024-02-21 09:58:57'};
params.put('input', inputParams);
Object result = formatter.call(action, params);
Map<String, Object> resultMap = (Map<String, Object>) result;
Datetime formattedDateTime = (Datetime) resultMap.get('result');
System.assertEquals(2024, formattedDateTime.year());
System.assertEquals(2, formattedDateTime.month());
System.assertEquals(21, formattedDateTime.day());
}
} How to use Custom Function in the Action Syntax :- Class:fx_methodName(/path)
Example :- CustomDateTimeFormatter:fx_formatDateTime(/StartDate) In the value side, you can use the above syntax to set the value accordingly.
Last updated