Explore more
Functions
Custom functions
15min
this feature is available to enterprise customers custom functions allow you to enhance the built in data transformation capabilities available in the {{scenario singular lowercase}} designer custom functions are created using javascript {{product name}} supports the infrastructure and system behavior that takes care of managing and using custom functions however, our customer care team cannot provide support on how to write and debug your own custom functions create and manage custom functions you create and manage custom functions in the functions section custom functions belong to a team all users with the team member role can view and use custom functions to create and edit custom functions, you need to have the team admin role functions overview to create a new custom function in the left sidenav, click functions click + add a function enter a name and description the name and description will appear in the list of functions available in the scenario designer click save write the code of the function use the keyword return to define the return value write a function click save when you save the function for the first time, it immediately becomes available in the scenario designer custom functions are marked with an icon to distinguish them from the built in functions basic structure of custom functions the structure of custom functions reflects the structure of regular javascript functions each function must contain the following function header the name of the function and the parameters it accepts function body enclosed in curly brackets exactly one return statement supported languages custom functions support javascript es6 no 3rd party libraries are supported the code runs on {{product name}} 's back end this means that we do not support functionality that only makes sense in the context of writing browser executed javascript validation of custom functions {{product name}} does not validate the code of your functions if there are errors in your code, they will appear as errors in the execution of scenarios that use these functions invalid functions will cause your scenarios to fail limitations of custom functions the following limits apply to all custom functions that you create a single custom function can't run for more than 300 milliseconds the code of a custom function cannot have more than 5000 characters functions must be synchronous you can't run asynchronous code in your functions you can't make http requests from your functions you can't call other custom functions from inside of a custom function you can't use recursion in your custom functions in order to use a custom variable with an iterator, you need to set the value with the custom function in a set variable module before the iterator map the output of the set variable to the iterator custom functions version history every time you save a custom function, the system creates a new history record this allows you to compare versions and revert your function to a previous version function history select a history record to compare the function with the previous version you can only compare two consecutive versions click restore this version to revert to the version that you're currently viewing this creates a new version of the function with the content of the restored version work with dates inside custom functions custom functions are executed on {{product name}} 's servers therefore, whenever you work with dates inside a function, the dates will take into account the time zone set for your organization call built in functions from inside of your custom functions you can use {{product name}} 's built in functions in your custom function code built in functions are available as methods of the iml object the following example uses the length built in function function usebuiltinfunction(text){ return iml length(text); } debug custom functions you can execute your custom function code in the debug console, which is located under the code editor delete custom functions you can delete your custom functions in the list in the functions section delete function before you delete a function, click edit next to it and then switch to the scenarios tab here, you can check in which scenarios the function is used when you delete a function that is used in a scenario, the scenario will fail to execute on its next run the scenario that uses the deleted function will fail with the following error message failed to map '\<field name>' function '\<function name>' not found! examples hello world returns the string "hello world " function helloworld() { return "hello world!"; } count the number of working days in a month calculates how many working days there are in the month specified by the month's number and a year in the function's arguments does not take into account local holidays function numberofworkingdays(month, year) { let counter = 0; let date = new date(year, month 1, 1); const enddate = new date(date getfullyear(), date getmonth() + 1, 0); while (date gettime() < enddate gettime()) { const weekday = date getday(); if (weekday !== 0 && weekday !== 6) { counter += 1; } date = new date(date getfullyear(), date getmonth(), date getdate() + 1); } return counter; } calculate the number of days between two dates calculates the number of days between two dates passed to the function as arguments function numberofdays(start, end) { const startdate = new date(start); const enddate = new date(end); return math abs((startdate gettime() enddate gettime()) / (1000 60 60 24)); } randomization return a random greeting from an array of greetings accepts an array as an argument returns a random item from the array function randomgreeting(greetings) { const index = math floor(math random() greetings length); return greetings\[index]; }