Use the following code snippets to help you switch from writing flows in C# to writing flows in JavaScript.
C#
JS
C#
JS
C#
JS
C#
JS
C#
JS
C#
JS
C#
JS
C#
JS
C#
JS
C#
JS
Note: Both C# and JS return a -1 if not found.
C#
JS
Note: You should convert to lowercase or uppercase prior to string comparison. For example, converting an email address, vendor name, or payment type name to lowercase prior to comparison will make it case-insensitive and protect you against capitalization errors.
C#
JS
C#
JS
C#
Note: Remove() takes a startIndex and an optional count. If no count is provided, it removes through the end of the string.
This is destructive. It is deleting any characters after the start index. vTest is changed and overwritten, and the next time you access it, it will be the shortened string value.
JS
Note: Slice() and Remove() are essentially opposites but can be used to the same effect. With slice you tell it what to keep.
Slice() takes a startIndex and an optional endIndex, but unlike Remove(), it returns the string between the start and the end indexes, whereas Remove() removes.
If no endIndex is provided, slice(), preserves through the end of the string.
This is non-destructive. It returns a new string from the start index to the end index while leaving the original string intact. vTest remains unchanged. You have to assign this to a new variable in order to access it.
Use replace to strip out special characters. It's a useful way to make sure your string is sanitized and does not contain special characters that can cause problems.
C#
JS
Note: This is non-destructive.
C#
Note: Substring is one of the most risky functions to run on a string. If you don't use Math.Min, your code will fail.
To avoid a blind substring call:
Safe substrings and knowing max values:
You should know what the max value is and protect against truncation. Alternatively, if using a substring to get the first X characters, you need to protect against the Math.Min being smaller.
JS
Note: Although JavaScript has its own substring() method, use slice() instead. To learn why, see this article on JavaScript substring() vs slice().
Trim should be one of the most common functions used with strings. Be sure to use it to remove any leading or trailing whitespace that can cause issues. Whitespace has been known to cause support tickets and delays in onboarding, and is very tough to track down. Use Trim instead.
C#
JS
Always check for null or empty prior to using the value. You can then create a hierarchy of data, for example, checking allocation fields first and if blank/null then checking the expense detail fields and if blank/null then defaulting to something. This type of defensive coding will keep flows from failing or remediation tasks from being created for missing data.
C#
JS
C#
JS
C#
Note: String splits are also risky and can be a source of failures. Be sure to always do a Contains prior to a split to check that your string contains the character or string you are splitting on.
JS
Note: As with previous JS string methods, these are non-destructive to the original string and must be assigned to new variables.
If the string does not contain the character or string you are splitting on, it returns undefined.
C#
JS
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. Flows also rely upon the JArray Class, which represents a JSON array, in the Newtonsoft.Json assembly. See more info at JArray Class.
Arrays are much easier to work with in JavaScript than C#.
For example, for use with the In operator in a Filter step.
Note: Use extreme care when hardcoding something. Make sure it is future proof and will not need to be changed in the future.
C#
JS
C#
Note: If you see the JArray instance expected error, that means you are missing the (object) cast. Change it to the following:
JArray.FromObject((object)Step("lookup-project-number").Output).Where(r => r["project-number"]?.ToString()
JS
Note: No converting or casting is necessary in JavaScript.
C#
JS
C#
Note: Don't do int.Parse. Change it to a TryParse instead. Decimals, dates, and integers all need to be TryParse.
Int32.TryParse(
item.GLCo.ToString(), out Int32 glco);
JS
Note: sort() mutates the original array. The default sort order is ascending. The time and space complexity of the sort cannot be guaranteed as it depends on the implementation.
Lodash is another option:
C#
To access element in lookup:
Note: Make sure your lookup step returns something. Every lookup should have an if exists check before using the output.
JS
To access element in lookup:
C#
JS
C#
JS
Note: No casting is necessary in JavaScript.
C#
JS
let results = '';
flow.trigger.data.flags[0].system_flag_details.forEach(flag => {
if (flag.reasons) {
results += `${flag.reasons}: `
}
})
return results;
C#
JS
Note: The reduce() method executes a user-supplied reducer callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value. See the Mozilla developer docs for more information on the reduce() method.
C#
JS
C#
JS
C#
JS
Note: sort() mutates the original array, whereas _.orderBy() returns a new array.
Was this helpful? Give us feedback.