When writing & debugging Scriptable Functions, there are three primary areas to focus attention on in order to ensure your scripts are working well; use of language functionality, logs and return value.
Let's take a look at each of these in detail, paying particular attention to the fact that the way your scripts are written have a direct impact on your ability to debug.
Language Functionality
Use JavaScript's regular error handling in order to catch issues with your script.
Logs
You can easily write out to logs using the ScriptableFunctionLogger utility, which outputs to linear logs, which are searchable and sortable.
logger.WriteLog('Case already has external reference');
Return
Always Return! The return object is recorded as a "Result" type in the logs, and records the desired result, in order for it to be analysed or re-run if required.
Using the "Run" command
Where to get the details:
itemId - This is the unique Id of the item, often found in the item itself or by using debug console
typeId - The item type (e.g. lead, customer, sales order) as an integer enumerator
payload - Any additional data to be passed into the function
functionId - The function Id itself (for instantiating the logger, etc.)
companyId - The tenant Id, for any queries which require it, although, use API keys when possible
Analysing the logs
Enter the logs for the function either via the logs icon next to the function name in the grid, or the "Logs" button in editor mode.