In AL development for Microsoft Dynamics 365 Business Central, helper code units are a foundational design pattern used to encapsulate reusable logic. These code units act as modular libraries that simplify your main business logic by offloading common tasks, such as formatting, validation, or calculations, into clean, callable units. Let’s take a look at one of the most useful helper code units; Type Helper.

The Type Helper code unit in Microsoft Dynamics 365 Business Central is a powerful utility designed to assist with data formatting, localization, and type conversion tasks. There are also several math function included in the library as they are useful in API handling which is a common use case for this library.

Type Helper is part of the System.Reflection namespace. You will need to include a “using System.Reflection;” statement in your code. You can then create the Type Helper as a variable like this:

TypeHelper: Codeunit "Type Helper";

There are 74 procedures, and 2 obsolete procedures, in this super powerful code unit. Here is a brief breakdown of each procedure.

  1. Evaluate
    Evaluates a string value into a Variant variable, supporting Date, DateTime, Decimal, and Integer types with optional format and culture.
  2. GetLocalizedMonthToInt
    Converts a localized month name (e.g., “January”) to its corresponding integer (1-12).
  3. CompareDateTime
    Compares two DateTime values, returning 1, 0, or -1 depending on their relationship, with a small threshold for equality.
  4. FormatDate (Date, LanguageId)
    Formats a Date value according to a specified language ID.
  5. FormatDate (Date, Format, CultureName)
    Formats a Date value using a custom format string and culture name.
  6. FormatDateWithCurrentCulture
    Formats a Date using the current user’s culture.
  7. GetHMSFromTime
    Extracts hours, minutes, and seconds from a Time value.
  8. IsLeapYear
    Determines if a given Date falls in a leap year.
  9. GetOptionNo
    Returns the index of a value in an option string.
  10. GetOptionNoFromTableField
    Gets the option index for a value from a specific table field.
  11. GetNumberOfOptions
    Returns the number of options in an option string.
  12. OptionsAreEqual
    Checks if two option values are considered equal.
  13. IsNumeric
    Checks if a text value can be evaluated as a decimal number.
  14. GetField
    Retrieves a Field record for a given table and field number, if not obsolete.
  15. GetFieldLength
    Returns the length of a field in a table.
  16. TestFieldIsNotObsolete
    Throws an error if a field is marked as obsolete.
  17. IsPhoneNumber
    Validates if a text value matches a phone number pattern.
  18. GetUserTimezoneOffset
    Gets the current user’s timezone offset as a duration.
  19. GetUserClientTypeOffset
    Gets the timezone offset if the client type is Web.
  20. GetTimezoneOffset
    Gets the timezone offset for a specified timezone ID.
  21. EvaluateUnixTimestamp
    Converts a Unix timestamp (seconds since 1970-01-01) to a DateTime, adjusted for user timezone.
  22. EvaluateUTCDateTime
    Evaluates a UTC DateTime string into a DateTime variable.
  23. FormatDateTime
    Formats a DateTime value using a format string and culture.
  24. FormatUtcDateTime
    Formats a DateTime as a string in UTC using a specified format and culture.
  25. GetCurrUTCDateTime
    Returns the current UTC DateTime.
  26. GetCurrUTCDateTimeAsText
    Returns the current UTC DateTime as a formatted string.
  27. GetCurrUTCDateTimeISO8601
    Returns the current UTC DateTime as an ISO 8601 string.
  28. AddHoursToDateTime
    Adds a specified number of hours to a DateTime.
  29. FormatDecimal
    Formats a decimal value as a string using a format and culture.
  30. UrlEncode
    URL-encodes a text value.
  31. UrlDecode
    URL-decodes a text value.
  32. HtmlEncode
    HTML-encodes a text value.
  33. HtmlDecode
    HTML-decodes a text value.
  34. JavaScriptStringEncode (Value: Text)
    Encodes a string for safe use in JavaScript.
  35. JavaScriptStringEncode (Value: Text; AddDoubleQuotes: Boolean)
    Encodes a string for JavaScript, optionally adding double quotes.
  36. UriEscapeDataString
    Escapes a string for use in a URI.
  37. UriGetAuthority
    Gets the authority part of a URI.
  38. GetKeyAsString
    Returns a comma-separated string of key field names for a record variant and key index.
  39. ReadAsTextWithSeparator
    Reads an InStream as text, joining lines with a specified separator.
  40. TryReadAsTextWithSeparator
    Tries to read an InStream as text with a separator, returns success/failure.
  41. TryReadAsTextWithSepAndFieldErrMsg
    Tries to read an InStream as text, showing a message if it fails.
  42. CRLFSeparator
    Returns a CRLF (carriage return + line feed) separator.
  43. LFSeparator
    Returns a LF (line feed) separator.
  44. SortRecordRef
    Sorts a RecordRef by specified fields in ascending or descending order.
  45. TextDistance
    Calculates the Levenshtein distance (edit distance) between two text strings.
  46. NewLine
    Returns the system’s newline string.
  47. GetMaxNumberOfParametersInSQLQuery
    Returns the maximum number of parameters allowed in a SQL query (2100).
  48. BitwiseAnd
    Performs a bitwise AND operation on two integers.
  49. BitwiseOr
    Performs a bitwise OR operation on two integers.
  50. BitwiseXor
    Performs a bitwise XOR operation on two integers.
  51. GetFormattedCurrentDateTimeInUserTimeZone
    Returns the current date/time in the user’s timezone, formatted as specified.
  52. GetCurrentDateTimeInUserTimeZone
    Returns the current date/time in the user’s timezone as a DateTime.
  53. GetInputDateTimeInUserTimeZone
    Converts a DateTime from UTC to the user’s timezone.
  54. ConvertDateTimeFromUTCToTimeZone
    Converts a DateTime from the current client timezone to a specified timezone.
  55. ConvertDateTimeFromInputTimeZoneToClientTimezone
    Converts a DateTime from a specified timezone to the current client timezone.
  56. IntToHex
    Converts an integer to a hexadecimal string.
  57. Maximum
    Returns the maximum of two decimal values.
  58. Minimum
    Returns the minimum of two decimal values.
  59. TransferFieldsWithValidate
    Transfers field values from a temporary buffer to a target record, validating changes.
  60. CalculateLog
    Calculates the base-10 logarithm of a decimal number.
  61. GetAmountFormatLCYWithUserLocale
    Returns the amount format for the local currency using the user’s locale.
  62. GetAmountFormatLCYWithUserLocale (DecimalPlaces: Integer)
    Returns the amount format for the local currency with specified decimal places.
  63. GetAmountFormatWithUserLocale (CurrencySymbol: Text[10])
    Returns the amount format for a currency symbol using the user’s locale.
  64. GetAmountFormatWithUserLocale (CurrencySymbol: Text[10]; DecimalPlaces: Integer)
    Returns the amount format for a currency symbol with specified decimal places and user’s locale.
  65. GetAmountFormat (LocaleId: Integer; CurrencySymbol: Text[10])
    Returns the amount format for a locale and currency symbol.
  66. GetAmountFormat (LocaleId: Integer; CurrencySymbol: Text[10]; DecimalPlaces: Integer)
    Returns the amount format for a locale, currency symbol, and decimal places.
  67. GetAmountFormatDecimalPlaces
    Returns the amount format for a locale, currency symbol, and decimal places, considering currency position.
  68. GetDefaultAmountFormat
    Returns the default amount format string.
  69. GetDefaultAmountFormat (DecimalPlaces: Integer)
    Returns the default amount format string with specified decimal places.
  70. GetXMLAmountFormatWithTwoDecimalPlaces
    Returns the XML amount format with two decimal places.
  71. GetXMLDateFormat
    Returns the XML date format string.
  72. CopyRecVariantToRecRef
    Copies a record variant to a RecordRef.
  73. IsDigit
    Checks if a character is a digit.
  74. IsUpper
    Checks if a character is uppercase.

Now, you might not feel like a “bitwise and” function is something you need in your ERP platform. However, there are API’s and such that may require these functions for data handling, and I’m grateful to have them when needed.

If this is looking a lot like the type helper classes found in .NET Reflection, that is because it is. Business Central AL code compiles into .NET code on the server. We can’t use all the .NET libraries, but the kind developers at Microsoft have provided these functions to help us.

More details on Learn.Microsoft.com Code unit “Type Helper” | Microsoft Learn

We will see these helper procedures appear as we start delving into API handling and other advanced software topics. Let me know if you have used these before, and what applications you use them for.

One response to “Understanding AL Development: The Type Helper Code Unit”

Leave a comment

Trending