Rendering force:Field Inside Custom Markup?

Rendering force:Field Inside Custom Markup?

On October 18, 2025, Posted by , In Admin Tutorial,Apex, With Comments Off on Rendering force:Field Inside Custom Markup?

Question: I am trying to use <force:inputField> and <force:outputField> in my Lightning Component and want to wrap them in custom markup like <div> or <article> for styling as cards. However, when I place these components inside any markup, they do not render in Lightning Experience. They work fine when used at the top level or in a standalone Lightning app. Is there a way to make them render correctly inside custom markup, or is this a bug in Lightning Components?

This issue arises because <force:inputField> and <force:outputField> have dependencies that must be loaded at the top level of the component in Lightning Experience. When these components are placed inside additional markup like <div> or <article>, they sometimes fail to render, particularly when the component is used on a Lightning page through the App Builder. This behavior does not occur in standalone Lightning apps, indicating that the problem is related to how Lightning record pages initialize and load component dependencies.

Here is a simple working example that renders correctly:

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" controller="LightningCaseTestController">
  <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
  <aura:attribute name="recordId" type="Id" />
  <aura:attribute name="contact" type="Contact" default="{sobjectType: 'Contact'}"/>

  <force:outputField value="{!v.contact.Name}"/>
  <force:inputField value="{!v.contact.Name}"/>
</aura:component>

This component works because the <force:*Field/> components are not wrapped inside any additional markup.

However, when you try to wrap them inside custom markup for styling:

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" controller="LightningCaseTestController">
  <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
  <aura:attribute name="recordId" type="Id" />
  <aura:attribute name="contact" type="Contact" default="{sobjectType: 'Contact'}"/>

  <article>
    <force:outputField value="{!v.contact.Name}"/>
    <force:inputField value="{!v.contact.Name}"/>
  </article>
</aura:component>

The fields do not render at all. This happens because the component dependencies required by <force:inputField> and <force:outputField> are not automatically loaded when these components are nested inside custom markup on a Lightning record page.

A known workaround is to include a hidden top-level <force:inputField> outside of any custom markup. This “primes” the component and allows all other <force:*Field/> components to render correctly inside markup. For example:

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" controller="LightningCaseTestController">
  <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
  <aura:attribute name="contact" type="Contact" default="{sobjectType: 'Contact'}"/>
  <aura:attribute name="dummmyContact" type="Contact" default="{sobjectType: 'Contact'}"/>

  <!-- Hidden top-level field to load dependencies -->
  <force:inputField value="{!v.dummmyContact.Name}" class="slds-hide"/>

  <!-- Custom markup for styling -->
  <article>
      <force:outputField value="{!v.contact.Name}"/>
      <force:inputField value="{!v.contact.Name}"/>
  </article>
</aura:component>

In this approach, the hidden <force:inputField> ensures that all necessary dependencies are loaded. Then, the <force:outputField> and <force:inputField> inside <article> render correctly with full Lightning styling and behavior.

So, while this seems like a bug, the workaround is widely used in the community to allow <force:*Field/> components to be styled inside custom markup on Lightning record pages without losing built-in behaviors.

This method avoids having to use <ui:outputText> or <ui:input*> components, which would require manual SLDS styling and do not provide the standard Lightning field behaviors.

Kick Start Your Career with Real-Time Project-Based Salesforce Training

Our Salesforce Course is designed to provide a thorough understanding of the Salesforce platform, equipping you with the essential skills to thrive in the CRM industry. The curriculum includes vital modules such as Salesforce Admin, Developer, and AI, combining foundational knowledge with hands-on practice. By engaging in real-world projects and assignments, you’ll develop the expertise to address complex business challenges using Salesforce solutions. Our expert instructors ensure you gain both technical skills and industry insights necessary for success in the Salesforce ecosystem.

Along with technical knowledge, our Salesforce Training in Minneapolis offers personalized mentorship, exam preparation, and interview coaching to enhance your career prospects. You’ll have access to comprehensive study materials, live project experience, and dedicated support throughout your learning journey. Upon completing the course, you’ll be well-prepared for certification exams and equipped with the practical skills employers value. Begin your Salesforce career with us and unlock countless career opportunities. Sign up for a Free Demo today!

Comments are closed.