When creating Advanced PDF Templates in NetSuite, dealing with null values is a common challenge. Null fields can break your logic, especially in conditional statements. Here’s how to handle null values effectively in Freemarker.
Why Null Values Cause Issues
Freemarker will throw an error if you try to perform operations (like comparisons) on null fields. For example, this will fail if value is null:
html Copy code <#if value gt 10> Value is greater than 10 </#if>
Solutions for Handling Null Values
- Use
?has_contentfor Null Checks - The
?has_contentoperator checks whether a field has a value.
html Copy code <#if value?has_content && value gt 10> Value is greater than 10 </#if>
- Set Default Values Using
! - Use the
!operator to set a default value if the field is null.
html Copy code <#if (value!0) gt 10> Value is greater than 10 </#if>
- Combine Null Checks with Conditions
- You can combine null checks with logical conditions to ensure error-free execution.
html
Copy code
<#if value?has_content>
<#if value gt 10>
Value is greater than 10
</#if>
</#if>
Best Practices
- Always check for null values in fields before using them in calculations or comparisons.
- Use default values for optional fields to avoid unexpected null-related errors.
By proactively managing null values in your conditional statements, you can create more reliable and robust NetSuite PDF templates.