Skip to main content

Eliminating Conditional Logic in LWC Forms

About Us
Published by yuliya.dzemidchuk
23 April 2026

Replacing Hardcoded Forms with Metadata-Driven LWC Architecture

 

The “lwc:if” Problem

Building forms in Lightning Web Components seems straightforward - create input fields, add validation, wire up save logic. Then requirements arrive: "Show field A only for Category X," "Field B appears when Status is Y," "Section C is conditional on multiple criteria." The natural response is to wrap everything in lwc:if directives. One condition. Two conditions. Five conditions. Ten variations. Suddenly the template becomes unmaintainable:

<template>
    <lwc:if if={isCategoryX}>
        <lightning-input label="Field A"></lightning-input>
    </lwc:if>
    
    <lwc:if if={isStatusY}>
        <lightning-input label="Field B"></lightning-input>
    </lwc:if>
    
    <lwc:if if={isComplexCondition}>
        <div>
            <lwc:if if={nestedCondition1}>
                <!-- 20 more fields -->
            </lwc:if>
            <lwc:if if={nestedCondition2}>
                <!-- 15 more fields -->
            </lwc:if>
        </div>
    </lwc:if>
</template>

 

Adding a new form variation requires touching dozens of lwc:if blocks. Testing all paths is hopeless. This approach doesn't scale past 3-4 variations.

There's a better way: metadata-driven dynamic forms. Instead of hardcoding conditions, store form configurations in Custom Metadata Types. The component reads metadata and renders forms dynamically. One component can handle 10+ variations without hardcoded conditional rendering in the template.

 

The Core Problem: Form Variations Explosion

Real-World Scenario:
Consider an application where forms vary based on user selections or record characteristics. Different combinations of criteria determine which fields appear, which sections are visible, and which validation rules apply. With just two selection criteria having 3-5 possible values each, the number of potential combinations grows rapidly.

Traditional Approach Problems:

  • Combinatorial explosion: Multiple criteria create hundreds of conditional statements

  • Nested conditions: Complex boolean logic becomes unreadable

  • Code duplication: Similar field configurations repeated across variations

  • Testing nightmare: Every combination needs test coverage

  • Brittle code: Changing one variation affects others

  • Merge conflicts: Multiple developers touching the same template

  • Business logic in markup: Non-developers can't modify forms

  • Performance: Browser parses hundreds of hidden DOM elements

 

The Metadata-Driven Solution

Core Concept:
Store form definitions in Custom Metadata Type records. Each record defines one field's configuration: where it appears, how it's positioned, when it's visible, and how it behaves. The LWC reads metadata and builds the form dynamically at runtime.

What Metadata Controls:

  • Field Identity: Which Salesforce field to render (API name and source object)

  • Display Order: Sequence in which fields appear within sections

  • Layout Position: Column placement in grid layouts (left, center, right)

  • Section Grouping: Logical grouping of related fields

  • Visibility Conditions: Boolean expressions evaluated at runtime to show/hide fields

  • Field Behavior: Required, read-only, or editable states

  • Label Overrides: Custom labels replacing standard field metadata

  • Default Values: Pre-populated values for new records

How It Works:

  1. Component receives context parameters identifying which form to display

  2. Apex controller queries Custom Metadata filtered by those parameters

  3. Metadata records are returned describing field configurations

  4. Component evaluates visibility conditions against current record data

  5. Fields meeting conditions render in specified sections and positions

  6. User interactions update record data

  7. Form transitions trigger new metadata queries for different contexts

 

Implementation Pattern

Apex Controller:

@AuraEnabled(cacheable=true)
public static List<FormFieldConfig> getFormConfiguration(String contextId) {
    // Query metadata records defining form structure
    return [
        SELECT FieldName, SectionName, ColumnPosition, DisplayOrder,
               VisibilityCondition, IsRequired, LabelOverride
        FROM Form_Field_Metadata__mdt
        WHERE FormContext__c = :contextId
        ORDER BY DisplayOrder
    ];
}

 

LWC JavaScript (Simplified):

import { LightningElement, api, wire } from 'lwc';
import getFormConfiguration from '@salesforce/apex/FormController.getFormConfiguration';

export default class DynamicForm extends LightningElement {
    @api contextId;
    @api recordId;
    
    sections = [];
    recordData = {};
    
    @wire(getFormConfiguration, { contextId: '$contextId' })
    wiredConfiguration({ data, error }) {
        if (data) {
            this.sections = this.buildSections(data);
        }
    }
    
    buildSections(metadata) {
        const sections = new Map();
        
        for (const config of metadata) {
            // Evaluate visibility condition against record data
            if (!this.evaluateCondition(config.VisibilityCondition)) {
                continue; // Skip field if condition fails
            }
            
            // Group fields by section
            if (!sections.has(config.SectionName)) {
                sections.set(config.SectionName, { fields: [] });
            }
            
            sections.get(config.SectionName).fields.push({
                name: config.FieldName,
                label: config.LabelOverride || this.getStandardLabel(config.FieldName),
                required: config.IsRequired,
                column: config.ColumnPosition,
                value: this.recordData[config.FieldName]
            });
        }
        
        return Array.from(sections.values());
    }
    
    evaluateCondition(expression) {
        if (!expression) return true;
        // Parse and evaluate expression like "Status == 'Active' AND Type != 'Draft'"
        // Returns boolean result
    }
}

 

LWC Template:

<template>
    <template for:each={sections} for:item="section">
        <div key={section.name} class="form-section">
            <h3>{section.title}</h3>
            
            <div class="form-fields">
                <template for:each={section.fields} for:item="field">
                    <div key={field.name} class={field.column}>
                        <lightning-input
                            name={field.name}
                            label={field.label}
                            value={field.value}
                            required={field.required}
                            onchange={handleFieldChange}
                        ></lightning-input>
                    </div>
                </template>
            </div>
        </div>
    </template>
</template>

 

Key Observation:
Zero lwc:if directives for conditional fields. All variation logic resides in metadata and the evaluateCondition() method. Adding a new form variation means creating metadata records, not modifying component code.

 

Why This Approach Works

Technical Benefits:

  • Single Component, Multiple Forms: One LWC handles all variations without code duplication.

  • Declarative Configuration: Forms are defined through metadata rather than hardcoded logic.

  • Version Control Friendly: Metadata files are independent, reducing merge conflicts.

  • Testing Simplification: Test condition evaluation logic once, not every variation.

  • Performance: Only fields meeting conditions render. No hidden DOM overhead.

  • Runtime Flexibility: Conditions evaluate per record. Same form shows different fields based on data.

  • Centralized Logic: All visibility rules in metadata. Easy to audit and modify.

Business Benefits:

  • Trained administrators can modify forms without developer involvement

  • Faster iteration - change metadata, refresh page to see results

  • Reduced development time for new form variations

  • Lower maintenance cost - fewer components to maintain

  • Better documentation - metadata serves as formal specification

  • Audit trail via metadata deployment history

     

When NOT to Use This Approach

Like any architectural pattern, metadata-driven forms come with trade-offs. This approach isn't universally appropriate:

  • Simple Static Forms: Forms with few fields that never change don't justify metadata overhead.

  • Highly Interactive Forms: Forms requiring complex JavaScript interactions (drag-drop, real-time calculations) fight against metadata-driven rendering.

  • Custom UI Requirements: When each variation needs fundamentally different layouts or custom components, metadata adds complexity without benefit.

  • Performance-Critical Scenarios: Metadata evaluation adds runtime overhead. For high-frequency rendering, hardcoding may perform better.

  • Minimal Variation: Two or three simple conditions are clearer with lwc:if than metadata architecture.

  • Lack of Governance: Without controls on metadata modification, forms become inconsistent. This approach requires discipline.

     

Decision Framework: Should You Use Metadata-Driven Forms?

Consider These Factors:
How many form variations exist?

1-2 → Hardcode

3-5 → Consider metadata

6+ → Metadata strongly recommended

How often do forms change?

Rarely → Either works

Monthly → Metadata saves time

Weekly → Metadata essential

Who modifies forms?

Only developers → Either works

Admins with training → Metadata enables them

Business analysts → Metadata required

What is field complexity?

Standard inputs → Metadata works well

Custom components → Metadata gets complex

Heavy JavaScript interactions → Hardcode probably better

How many conditional fields?

<10 → lwc:if acceptable

10-30 → Metadata recommended

30+ → Metadata essential

Is form layout consistent?

Yes → Metadata perfect fit

Minor variations → Metadata works

Completely different → Separate components

 

Implementation Challenges

This approach introduces new complexities:

  • Expression Evaluation: Visibility conditions need runtime parsing. Options include custom parsers or third-party libraries. Each has security and performance implications.

  • Error Handling: Malformed metadata breaks forms silently. Requires validation: syntax checking, required field validation, circular dependency detection.

  • Performance at Scale: Evaluating numerous metadata records per render adds overhead. Solutions: caching, lazy evaluation, server-side filtering.

  • Type Safety: Field values stored as strings in metadata require conversion to proper types (Number, Boolean, Date, etc.).

  • Field Metadata Access: Component needs field labels, types, and picklist values. Requires wire adapters or Apex describe calls.

  • Testing Complexity: Testing metadata-driven components requires mocking metadata responses and validating expression evaluation separately.

  • Learning Curve: Team must understand both LWC and metadata-driven patterns. Expect initial productivity decrease.

  • Debugging Difficulty: Rendering issues could stem from metadata, expressions, or component logic. More investigation points.

     

Best Practices for Metadata-Driven Forms

If implementing this pattern:

  1. Start small - validate the approach with one form before expanding

  2. Document metadata structure thoroughly for future maintainers

  3. Implement validation rules on Custom Metadata Type to prevent invalid configurations

  4. Establish naming conventions for organizational consistency

  5. Cache metadata in components - avoid re-querying on every render

  6. Build preview tooling for administrators to validate configurations

  7. Version control metadata despite its declarative nature

  8. Create clear documentation for expression syntax and available operators

  9. Test expression evaluation thoroughly - edge cases break forms

  10. Monitor performance - log metadata query and evaluation times

  11. Plan migration strategy from hardcoded to metadata-driven carefully

  12. Establish governance around who can modify metadata and review processes

When to Commit:
Commit to metadata-driven forms when forms are central to the application, variations are numerous, changes are frequent, and non-developers need modification capability. If forms are peripheral, variations are few, or changes are rare, simpler hardcoded approaches make more sense.

The worst outcome is partial implementation: building metadata infrastructure while reverting to hardcoded conditions when complexity increases. This creates dual maintenance burdens. Commit fully or don't start.

Metadata-driven dynamic forms solve a specific problem: managing numerous form variations without drowning in conditional logic. The solution introduces complexity - metadata design, expression evaluation, testing challenges - but eliminates the explosion of conditional directives that makes traditional approaches unmaintainable at scale. Choose this pattern when the problem justifies the solution, and commit to implementing it properly. Half-measures create technical debt worse than the original problem.


Alexander Zherebilo
Certified Salesforce Developer
image
Expertise
Question to the expert
image

We have available resources to start working on your project within 5 business days

1 UX Designer

image

1 Admin

image

2 QA engineers

image

1 Consultant

image
Related Articles
All articles
image
How to Customize Salesforce Field Service Mobile for Your Technicians
Salesforce Field Service Mobile provides a standard technician experience that companies can extend to fit their own service processes. This article looks at areas where a custom mobile workflow can help: AI repair assistance, field documentation, external tools, back-office integrations, quotes and payments on site, barcode scanning and role-based mobile layouts. It also outlines four practical steps for planning a mobile workflow around the technician’s work.
21 October 2026
image
Salesforce Field Service Mobile: New Capabilities for Technicians
Salesforce Field Service Mobile continues to evolve with new capabilities for technicians working in the field. Explore how updates to job preparation, mobile forms, voice input, mapping, and Work Order views are changing the technician experience, and learn why configuring these capabilities around your service operations matters.
14 October 2026
image
Closing the Gap Between Field Service and Business Needs
Standard Salesforce Field Service configuration covers many service processes, but individual businesses can have different workflows, working conditions, scheduling rules, and operational requirements. This article looks at three areas where standard configuration can fall short: how technicians work, the conditions of field service operations, and business-specific processes. It also provides a practical checklist of signs that a Salesforce Field Service setup could benefit from customization.
07 October 2026