Dynamic fields allow you to calculate, combine or transform values using Visual Basic code. You can work with fixed values, reference existing data fields or use functions to process data.
The following examples illustrate common operations in dynamic fields.
Note
dox42 supports a customised implementation of Visual Basic. Not all standard Visual Basic features and functions may be available.
Using .NET functions
When using Visual Basic .NET functions in dynamic fields, you may need to specify the fully qualified name, including the namespace and class.
Combining text values
Data sources often store related information in separate data fields. For example, an employee's first name and last name may be stored individually, while a generated document requires the full name as a single value.
You can use a dynamic field to combine these values into one text string. This is particularly useful if you want to reuse the combined value in different parts of a document or process it further.
Example: Combining a first name and a last name
The following example combines the FirstName and LastName data fields from the EmployeeNames data source.
result = EmployeeNames.FirstName + " " + EmployeeNames.LastNameHow the example works:
EmployeeNames.FirstNameretrieves the employee's first name.+combines the individual text values." "adds a space between the two names.EmployeeNames.LastNameretrieves the employee's last name.result =assigns the combined value to the dynamic field.
For example, if FirstName contains Alana and LastName contains Smith, the dynamic field returns: Alana Smith.
You can also combine data fields with fixed text, such as labels or prefixes, by enclosing the fixed text in double quotation marks.
Replacing text in a data field
Data retrieved from external systems may contain characters or text that do not match the formatting requirements of a generated document.
You can use a dynamic field to replace specific characters or text without modifying the original data source. It can also be used to remove unnecessary characters.
Example: Formatting an article number
An ERP system stores article numbers using forward slashes as separators, for example, AB/2026/015. In the generated document, the article number should appear with dots instead: AB.2026.015.
The following example replaces every forward slash in the ArticleNumber data field from the Products data source with a dot.
result = Products.ArticleNumber.Replace("/", ".")How the example works:
Products.ArticleNumberretrieves the article number from the data source..Replace("/", ".")replaces every forward slash (/) with a dot (.).result =assigns the modified article number to the dynamic field.
For example, if the original article number is AB/2026/015, the dynamic field returns AB.2026.015.
The original article number in the data source remains unchanged. Only the value returned by the dynamic field is modified.
You can also use Replace() to replace longer text strings. The first argument specifies the text to find, and the second argument specifies its replacement.
Note
The
Replace()function is case-sensitive when replacing letters or words.
Checking and shortening text values
You can use a dynamic field to check the length of a text value and shorten it if it exceeds a specified number of characters. This is useful when text from a data source must fit within a defined character limit.
Example: Limiting text to ten characters
Suppose a data source contains product descriptions that must not exceed ten characters in a generated document. You can use a dynamic field to shorten longer values while keeping shorter values unchanged.
Example: Limiting text to ten characters
Dim var1 as String =Datasource.Datafield
If var1.Length > 10 Then
result = var1.Substring(0,10)
Else
result = var1
End IfHow the example works:
Datasource.Datafieldretrieves the original text value.var1stores the value as a string.var1.Lengthdetermines the number of characters in the text.If var1.Length > 10 Thenchecks whether the text exceeds ten characters.var1.Substring(0,10)extracts the first ten characters if the text is too long.Elseensures that values containing ten characters or fewer remain unchanged.result =assigns the resulting text to the dynamic field.
Calculating values from data fields
Data sources often provide individual numeric values that need to be combined in a calculation before they can be used in a generated document. For example, an order data source may contain the quantity of an ordered product, while a product data source provides its unit price.
You can use a dynamic field to perform calculations using values from existing data fields and return the calculated result for use in your document template.
Example: Calculating the total price of an order item
The following example multiplies the quantity of an ordered product by its unit price to calculate the total price for that order item.
result = orders.quantity * product.priceHow the example works:
orders.quantityretrieves the ordered quantity from theordersdata source.product.priceretrieves the unit price from theproductdata source.The multiplication operator
*multiplies the two values.result =assigns the calculated total price to the dynamic field.
For example, if the ordered quantity is “5” and the unit price is “19.95”, the calculated total price is 99.75.
Formatting the calculated value
You can use the Field Formatter to define how the calculated value is displayed in the generated document, for example, with two decimal places or a currency symbol. For more information, see Culture and number/date formatting.
Converting text values to integers
Data fields in dox42 are interpreted as strings by default. If you need to use a data field value as an integer, you can convert it using the Integer.Parse() function.
Example: Converting a number of months into an integer
Suppose a data source contains the number of months as a text value. You can convert this value into an integer so that it can be used in subsequent numeric operations.
Dim observedMonthsAsInt As Integer = Integer.Parse(months.ID_month)How the example works:
months.ID_monthretrieves the value from the data source.Integer.Parse()converts the text value into an integer.Dim observedMonthsAsInt As Integerdeclares an integer variable to store the converted value.
For example, if months.ID_month contains the text value "12", the variable observedMonthsAsInt will contain the integer “12”.
Processing values conditionally
You can use an If...Then...Else statement in a dynamic field to return different values depending on a condition. This is useful when the content of a generated document needs to change based on information retrieved from a data source.
Example: Selecting a form of address
Suppose a customer data source contains a field indicating the customer's gender. You can use a dynamic field to select the appropriate form of address.
if customer.sex = "f" then
result = "Miss"
else
result = "Mister"
end ifHow the example works:
customer.sexretrieves the value from the customer data source.If customer.sex = "f" Thenchecks whether the field contains the value "f".result = "Miss"returns "Miss" if the condition is met.Elsedefines what happens if the condition is not met.result = "Mister"returns "Mister" for all other values.End Ifcloses the conditional statement.
For example, if customer.sex contains "f", the dynamic field returns “Miss”. For any other value, it returns “Mister”.
Note
The
Elsebranch applies to every value other than "f", including empty or unexpected values.
Generating a timestamp for testing and tracking
You can use a dynamic field to generate a timestamp that records the date and time at which the code is executed. This can be useful for testing, tracking execution times or investigating performance issues.
Example: Generating an execution timestamp
The following example retrieves the current date and time and combines its individual components into a timestamp.
Dim stamp As DateTime = DateTime.Now
Dim year As String = stamp.Year
Dim month As String = stamp.Month
Dim day As String = stamp.Day
Dim hour As String = stamp.Hour
Dim minute As String = stamp.Minute
Dim second As String = stamp.Second
Dim millisecond As String = stamp.Millisecond
result = year + "-" + month + "-" + day + "_" + hour + "-" + minute + "-" + millisecondHow the example works:
DateTime.Nowretrieves the current date and time.stamp.Year,stamp.Monthandstamp.Dayextract the date components.stamp.Hour,stamp.Minute,stamp.Secondandstamp.Millisecondextract the time components.result =assigns the generated timestamp to the dynamic field.
The individual values are combined into a single string, separated by hyphens and an underscore. For example, the resulting value could be:
2025-3-3_16-51-677
Accessing data fields with special naming conventions
You can use the GetValue() function to access data fields that cannot be referenced using the usual data source and field name syntax. This is necessary when field names contain characters that are not supported in direct field references, such as German umlauts.
The function is also useful when working with fields that follow special naming conventions, such as LinkedEntity fields retrieved from Microsoft Dataverse using FetchXML.
Example: Accessing a LinkedEntity field
The following example retrieves the value of a contact's full name.
return GetValue("contact.fullname")How the example works:
GetValue()retrieves the value of a data field by its specified name."contact.fullname"identifies the field to retrieve.returnreturns the retrieved value.
Example: Accessing a field containing special characters
The following example retrieves the value of a data field whose name contains a German umlaut:
result = GetValue("DS.DatenSchutzErklärung")In this case, GetValue() is required because the field name cannot be referenced directly in dox42.
Decoding Base64-encoded images
You can use Visual Basic to decode Base64-encoded image data retrieved from a data source. This is useful when images are provided as Base64 strings rather than as image files.
Example: Decoding an image from a data source
Suppose a data source provides a product image as a Base64-encoded string. You can use the FromBase64String() function to decode the string into binary image data.
result = System.Convert.FromBase64String(Datasource.Datafield)How the example works:
Datasource.Datafieldretrieves the Base64-encoded image data from the data source.System.Convert.FromBase64String()converts the encoded string into a byte array.result =assigns the decoded data to the dynamic field.
In the dox42 Data Fields explorer, select Image as the type of the dynamic field to configure it for image content. See Inserting data fields.
Retrieving binary data from a data field
The GetObject() function retrieves the value of a data field as a byte array (BLOB). This can be useful when a data source contains binary data, such as an image.
Example: Retrieving an image stored as binary data
The following example retrieves an image stored as a BLOB in a data source.
result = GetObject("DS.Image")How the example works:
GetObject()retrieves the value of the specified data field as a byte array."DS.Image"identifies the data field containing the image data.resultreceives the retrieved byte array.