Skip to main content

Latest Post

Custom Metadata Types vs Custom Settings vs Custom Labels

  Custom Metadata Types vs Custom Settings vs Custom Labels 💬 In plain words:  Three lookalikes: Custom Metadata = configuration that DEPLOYS with your code (best for app settings). Custom Settings = org/user-specific values, changeable at runtime (hierarchy type is great for bypass switches). Custom Labels = translatable text for the UI. 📌 Example:  API endpoint URLs per environment → Custom Metadata (deploys with code, sandbox vs prod values). A 'Bypass_Automation__c' checkbox an admin flips during data load → hierarchy Custom Setting. The word 'Submit' translated to Hindi → Custom Label. 🎬 Real-Life Example: The Fee Table Trapped Inside Code  Skyline charges a different delivery fee per city. Apex needs those rates on every booking. The Old/Bad Way:  if (city == 'Delhi') fee = 50. Else if (city == 'Mumbai') fee = 65. … Every rate change is a code change, a test run, and a deployment. Why this is bad:  Business data is trappe...

Building a Dynamic Device Form Factor in LWC for Salesforce

Introduction:

In today's rapidly evolving digital landscape, creating user-friendly and responsive interfaces is crucial. Salesforce Lightning Web Components (LWC) provide a powerful framework for building interactive and efficient applications. In this blog post, we will explore how to develop a dynamic device form factor in LWC, allowing your application to adapt seamlessly across various screen sizes and devices.


Prerequisites:


Before we dive into the implementation, make sure you have a basic understanding of Salesforce, Lightning Web Components, and web development concepts such as HTML, CSS, and JavaScript.


Setting Up the Project:


1. Create a new Lightning Web Component in your Salesforce org using the Salesforce CLI or Salesforce Developer Console.


Implementing the Dynamic Device Form Factor:


Step 1: Fetching the Device Form Factor


To create a responsive layout, we need to determine the current device form factor (desktop, tablet, or phone). Salesforce provides a global object, $Browser, which we can utilize to fetch the device form factor.


import { LightningElement, api } from 'lwc';


export default class DeviceFormFactor extends LightningElement {

    @api deviceFormFactor;


    connectedCallback() {

        this.deviceFormFactor = this.getDeviceFormFactor();

    }


    getDeviceFormFactor() {

        if (typeof window !== 'undefined' && window.innerWidth) {

            if (window.innerWidth >= 960) {

                return 'DESKTOP';

            } else if (window.innerWidth >= 600) {

                return 'TABLET';

            } else {

                return 'PHONE';

            }

        }

        return 'DESKTOP';

    }

}


Step 2: Implementing the Dynamic Layout


Next, let's create a dynamic layout that adjusts based on the device form factor. We will leverage the CSS Flexbox module to achieve this.


<template>

    <div class={dynamicLayout}>

        <!-- Place your dynamic content here -->

    </div>

</template>


<style>

    .dynamicLayout {

        display: flex;

        flex-direction: row;

        /* Customize the layout based on the device form factor */

        /* e.g., for tablet or phone */

        @media (max-width: 959px) {

            flex-direction: column;

        }

    }

</style>


Step 3: Using the Dynamic Layout Component


Finally, let's incorporate our dynamic layout component into your existing LWC component.


<template>

    <lightning-card title="Dynamic Device Form Factor">

        <c-device-form-factor device-form-factor={deviceFormFactor}></c-device-form-factor>

    </lightning-card>

</template>


Congratulations! You have successfully implemented a dynamic device form factor in your Lightning Web Component.


Conclusion:


By creating a dynamic device form factor in your LWC, you can ensure that your Salesforce applications provide an optimal user experience across various devices and screen sizes. This responsive design approach enhances usability and accessibility, ultimately leading to increased user satisfaction.


Remember to continuously test your implementation on different devices and adapt the layout as needed. Salesforce Lightning Web Components offer extensive possibilities for building powerful and flexible applications, so feel free to explore additional features to further enhance your project.


Happy coding!

Popular Posts

Salesforce LWC Code for Multi-Select Lookup

Introduction: In Salesforce Lightning Web Components (LWC), implementing a multi-select lookup field can enhance the user experience and provide greater flexibility for selecting multiple related records. In this blog post, we will walk through the process of creating a multi-select lookup field using LWC. We will cover the required code snippets and provide step-by-step instructions to help you implement this functionality in your Salesforce org.