---
schema_version: "1"
id: 14a30f6b0d6d62d7e901e4892f5de96a0497b11496b61b284528376e29afda95
title: Sample Apex Code for Custom Functions
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/xcloud/264-0-0/os_sample_apex_code_for_custom_functions.md
canonical_url: https://help.salesforce.com/s/articleView?id=xcloud.os_sample_apex_code_for_custom_functions.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 7ebd192c49df6a629387947bb799c62ea7919754b1a56ae0ad8142db815fed03
description: Develop Apex classes to implement custom functions for use with Omnistudio Data Mappers and Integration Procedures. A sample Apex class implements three methods that can be called as custom functions in Omnistudio formulas. The class includes code that passes a list to a custom function.
products:
  - Omnistudio
release: "264"
---
# Sample Apex Code for Custom Functions

Develop Apex classes to implement custom functions for use with Omnistudio Data Mappers and Integration Procedures. A sample Apex class implements three methods that can be called as custom functions in Omnistudio formulas. The class includes code that passes a list to a custom function.

Data Mappers and Integration Procedures can invoke these functions to convert a list to an array, replace one character with another, or reformat phone numbers. The class and `invokeMethod` of custom functions must be global.

## The convert function

The first custom function, `convert`, converts a list of values without keys to an array of values with keys. The function signature is:

```
FUNCTION('MyCustomFunctions', 'convert', LIST(%Inputlist%), %ListKey%)
```

If the list key is `"Item"` and the input list is:

```
{
  "InputList": [ "abc", "def", "ghi" ]
}
```

The result is:

```
{
  [
    {
      "Item": "abc"
    },
    {
      "Item": "def"
    },
    {
      "Item": "ghi"
    }
  ]
}
```

## The replace function

The second function, `replace`, replaces a character in a string with another character. The function signature is:

```
FUNCTION('MyCustomFunctions', 'replace', %InputString%, %ToReplace%, %ReplaceWith%)
```

If the input string is `my-input-string`, the character to replace is `-`, and the character to replace it with is `_`, the result is `my_input_string`.

## The formatPhoneNumber function

The third function, `formatPhoneNumber`, converts a phone number to the format `(nnn) nnn-nnnn`. The number must have no country code, a 3-digit area code, a three-digit exchange, and an overall length of at least nine digits. The function signature is:

```
FUNCTION('MyCustomFunctions', 'formatPhoneNumber', %Phone%)
```

If the input is `123.456.7890`, `123-456-7890`, or `1234567890`, the result is `(123) 456-7890`.

## Passing a list to a custom function

If input to your custom function is of the type `List<Object>`, Apex saves the input to a `VLOCITY-FORMULA-LIST` key. To retrieve the input, use code that retrieves this key.

```
static List<Map<String, Object>> FUNCTION(List<Object> inputs)
{
  List<Object> listInput = ((Map<String, Object>)inputs[0]).get('VLOCITY-FORMULA-LIST');
  Object variableInput = inputs[1];
              
  // Rest of function
}
```

For a working example that passes a list to a custom function, see the implementation of the `convert` function in the example code.

## The Apex class

The custom functions just described are defined as methods of the class `MyCustomFunctions`. For more information about calling custom functions and examples of the three custom functions defined in the class, see [Omnistudio Invocation Functions](https://help.salesforce.com/s/articleView?id=xcloud.os_omnistudio_invocation_functions.htm&release=264.0.0&type=5 "Functions that invoke or are related to invoking other functions and operations.").

```
global class MyCustomFunctions implements Callable
{
        
    /*
     * input  - arguments - List<Object> of arguments passed to the function
     * output - result    - The function supports single Object values, List<Object>,
     *                      or Map<String, Object>
     */
        
    public Object call(String action, Map<String, Object> args)
    {
        Map<String, Object> input = (Map<String, Object>) args.get('input');
        Map<String, Object> output = (Map<String, Object>) args.get('output');
        Map<String, Object> options = (Map<String, Object>) args.get('options');
        return invokeMethod(action, input, output, options);
    }
        
    global Boolean invokeMethod(String methodName, Map<String, Object> inputs,
        Map<String, Object> output, Map<String, Object> options)
    {
        if (methodName == 'convert') {
            List<Object> arguments = (List<Object>) inputs.get('arguments');
            output.put('result', convert(arguments));
        } else if (methodName == 'replace') {
            List<Object> arguments = (List<Object>) inputs.get('arguments');
            output.put('result', replace(arguments));
        } else if (methodName == 'formatPhoneNumber') {
            List<Object> arguments = (List<Object>) inputs.get('arguments');
            output.put('result', formatPhoneNumber(arguments));
        }
        return true;
    }
        
    private LIST<Map<String, Object>> convert(List<Object> arguments)
    {
        try {
            LIST<Map<String, Object>> result = new LIST<Map<String, Object>>();
            Map<String, Object> inputlist = (Map<String, Object>) arguments[0];
            String key = (String) arguments[1];
            List<Object> listofElements = (List<Object>) inputlist.get('VLOCITY-FORMULA-LIST');
            for (Object str : listofElements) {
                result.add(new Map<String, Object>{key => str});
            }
            return result;
        } catch(Exception e) {
            return new LIST<Map<String, Object>>(); 
        }
    }
        
    private String replace(List<Object> arguments)
    {
        String result = '';
        String inputlist = (String) arguments[0];
        String toreplace = (String) arguments[1];
        String replaceby = (String) arguments[2];
        result = inputlist.replaceAll(toreplace, replaceby);
        return result;
    }
        
    public string formatPhoneNumber(List<Object> arguments)
    {
        String Phoneno = arguments[0] + '';
        String formattedPhone = Phoneno.replaceAll('[^0-9+]', '');
        String NewFormat;
        if (String.isNotBlank(formattedPhone) && formattedPhone.length() >= 9) {
            String first = formattedPhone.subString(0, 3);
            String second = formattedPhone.subString(3, 6);
            String third = formattedPhone.subString(6);
            NewFormat = '(' + first + ')' + ' ' + second + '-' + third;
        }
        return NewFormat;
    }
        
}
```