Handling Deleted Fields in Extensibility Forms

Overview

When editing extensibility forms, it is common for fields to be removed or renamed as requirements evolve. However, simply deleting a field from the form definition does not automatically remove its stored values from the database. This can lead to errors when updating records that still contain values for fields that no longer exist in the form definition.

To address this, K2BTools introduces a controlled mechanism for handling deleted fields, ensuring data integrity and security while giving developers explicit control over stored data.

Key Principles

  1. Security:
    Prevent users from injecting arbitrary or obsolete values into the database by ensuring only values for defined fields are stored.

  2. Data Preservation:
    When a field is deleted from a form, its stored values are not automatically removed. This prevents accidental data loss in case the deletion was unintentional.

  3. Explicit Cleanup:
    Developers are responsible for cleaning up obsolete values from the database when a field is intentionally removed from a form.

Cleanup Mechanism

CleanupNonExistentFields Procedure

A new procedure, CleanupNonExistentFields, is provided in the K2BTools basic objects module. This procedure receives the current values and the form definition, and returns an updated collection containing only the values for fields that still exist in the form.

Usage Example

Below is a typical usage pattern for cleaning up obsolete field values in your database:

&FormDefinition = K2BTools.IntegrationProcedures.LoadFormStructureByRef(ExtFormType.Transaction, !"Customer") 
For Each Customer 
    &FormValues.FromJson(CustomerExtendedAttributes) 
    CleanupNonExistentFields(&FormDefinition.Structure, &FormValues) 
    CustomerExtendedAttributes = &FormValues.ToJson() 
EndFor 
Commit
  • Step-by-step:

    1. Load the current form definition using LoadFormStructureByRef.

    2. Deserialize the stored JSON values into an SDT.

    3. Call CleanupNonExistentFields to remove values for fields that no longer exist in the form definition.

    4. Serialize the cleaned SDT back to JSON and store it.

Best Practices for Field Deletion

  • Mark as Invisible:
    If you want to remove a field from the UI but retain its data, mark the field as invisible in the form definition. This allows you to preserve the data while hiding it from users.

  • Permanent Deletion:
    If you are sure a field should be permanently removed, delete it from the form definition and use the CleanupNonExistentFields procedure to remove its values from the database.

Summary

  • Fields deleted from a form are not automatically removed from stored data.

  • Use the CleanupNonExistentFields procedure to explicitly remove obsolete values.

  • Mark fields as invisible if you want to hide them without deleting their data.

  • Always review and clean up stored data when making structural changes to forms.