C# to JavaScript Conversions

Use the following code snippets to help you switch from writing flows in C# to writing flows in JavaScript.

DateTime

Return today’s date:

C#

JS

Compare a date to the current date:

C#

JS

Safely convert a value to a date, or default to today’s date:

C#

JS

Calculate the next closest Friday after a given date. Replace the example date with your date variable:

C#

JS

Calculate the last day in a month:

C#

JS

Calculate the last day in a week:

C#

JS

Compare two dates:

C#

JS

Use Vista standard formatting for batch creation (set to 1st day of current month):

C#

JS

Create a DateTime variable, set it equal to an input value, and output it in Vista format:

C#

JS

Strings

Determine if a string contains specified text:

C#

JS

Find the first position of a specified value:

Note: Both C# and JS return a -1 if not found.

C#

JS

Convert to upper or lower case

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

Return the length of the string:

C#

JS

Delete all characters from specified index to end:

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.

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. 


Replace all instances of a string with another:

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. 

Return part of the string:

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().

Remove starting and ending white space:

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

Evaluate if a string is blank or null:

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

Format data as currency:

C#

JS

Split a string into parts:

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.


Add characters to the beginning or end of a string:

C#

JS

Arrays

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#.

Create a hardcoded array:

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

Convert a list output to a JArray:

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.


Determine if any element in an array satisfies a requirement:

C#

JS

Sort an array by a column in descending order and then return the first value (highest value):

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:

Evaluate if a lookup step has any records:

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:

Make sure all lines of a JArray/array contain a value matching the criteria:

C#

JS

Cast the dynamic object from a filter step's output to an object so you can turn it into a JArray:

C#

JS

Note: No casting is necessary in JavaScript.

Add each value in an array to a list or a string:

C#

JS

let results = '';

flow.trigger.data.flags[0].system_flag_details.forEach(flag => {

  if (flag.reasons) {

    results += `${flag.reasons}: `

  }

})

return results;

Sum all values in a field of an array or list:

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.

Assign a value based on looking up a value in an array:

C#

JS

Determine if an object in an array has a field:

C#

JS

Sort the input for a Map step to intentionally order lines in an invoice:

C#

JS

Note: sort() mutates the original array, whereas _.orderBy() returns a new array.