Tag: Nested

  • What does Salesforce’s Collection mean in 2023?

    What does Salesforce’s Collection mean in 2023?

    There are different kinds of collections in Salesforce that can hold a large number of records. Collections are thus sets of documents that belong to the same type. Depending on the demands of the business, collections might dynamically increase and decrease. Lists, sets, or maps can all be collections in Apex. The number of records that can be kept in a collection is unbounded. 

    Since there was no software that could manage thousands of customers’ records in one location, businesses previously had trouble keeping records for different clients. Salesforce collections made things better. By consolidating customer records in one location using Salesforce collections, businesses were able to increase customer satisfaction. 

    Different Collections in Apex

    There are three distinct collections in Salesforce:

    • Collection of list  
    • Set accumulation 
    • Collecting maps 

    Lists Collection

    One of the most significant types of collection is a list, which can contain any kind of data. A list is an ordered collection of any type of data, including user-defined types, primitive types, collections, sObjects, and built-in Apex types.

    dont miss out iconDon’t forget to check out: Learn All About Collections in Salesforce: List, Set and Map

    The following are a list collection’s main characteristics in Salesforce:

    • In a list collection, duplicates and null values are acceptable.
    • The keyword to declare a list collection is “List“.
    • The initial entry in a list collection always has index position zero (0).
    • A collection of Apex lists has the capacity to expand dynamically over time.
    • The data type in a list collection might be either primitive or non-primitive.
    • The keyword must be used within the <> characters, followed by the data type to define a list collection.

    The syntax for a list is as follows:

    List<datatype> listName = new List<datatype>();

    Set Collection

    An unordered collection is a set. It is a singular collection of values without any duplications. Choose this collection if you don’t want any duplicate items in your collection.

    The following is a set collection’s primary characteristics in Salesforce:

    • A set collection can contain any data type, including primitive data types and sObjects.
    • An index does not exist for a set collection.
    • There are never any duplicates or null values in a set collection.
    • In Salesforce Apex, set collections are not frequently used.
    • Collections that are nested inside of one another can be stored as sets.
    • To declare a set collection, you must use the set keyword and the name of the primitive data type enclosed in <> characters.

    The syntax for a set is as follows:

    Set<datatype> setName = new Set<datatype>();

    dont miss out iconCheck out another amazing blog here: Implementing Data Collection for Impact Evaluations | Salesforce Guide

    Maps Collection

    A key-value combination called a “map” comprises the distinct key for each value. When anything must be rapidly located, it is used. 

    The essential components of a map collection in Salesforce are as follows: 

    • In a map collection, the key and value can both be any type of data.  
    • The null value may be kept in a map key in a map collection. 
    • In a map collection, keys of the type string are case-sensitive. 
    • To declare a map collection, you must use the map keyword, followed by the key and value data types surrounded in <>. 

    The syntax for a map is as follows:

    Map<datatype_key,datatype_value> mapName = new Map<datatype_key,datatype_value>();
  • Communication between Components in LWC | The Ultimate Guide

    Communication between Components in LWC | The Ultimate Guide

    In this blog, we will understand the communication of components in LWC. To create dynamic and flexible communication of components with each other your lightning web component is required. Since we know that the lwc component can be nested, there are 3 options for communication between components.

    Three Types of communication between the components:-

    1.  Parent to child communication
    2.  Child to parent communication
    3.  Communication between two separate components

    Parent To Child Communication

    We need to use the @api decorator to expose the children’s properties/methods for the parent to call directly using JavaScript API.

    ChildComponent.js

    @api
    chnageMessage(msgString){
    this.Message = msgstring.toUpperCase();
    }
    ParentComponent.js
    this.template .querySelector('c-Child-Component').changeMessage(event.target.value);

    dont miss out iconDon’t forget to check out: How do you Add LWC Component to Action Button?

    Child-to-Parent Communication

    In Lightning Web Components, a custom event is used to communicate from a child component to a parent component. With LWC we can create and send custom events.

    Create Event in ChildComponent js file:

    onChnageEvent(event){
    const selectEvent = new CustomEvent('customevent', {detail: Value_To_Be_PasseD});
    this.dispatchEvent(selectEvent);
    }

    Handling event in Parent Component:

    ParentCmp.html
    <c-child-comp oncustomevent = {handleCustomEvent}></c-child-comp>
    ParentCmp.js
    handleCustomEvent(event){
    const textVal = event.detail;
    this.msg = textVal;
    }

    Communication between two Separate Components

    You can use the pubsub library or the flash messaging service to communicate between components that are not in the same DOM hierarchy. These methods can be used to communicate between sibling components in an outer lwc component or between two lwc components  that are dropped separately in lightning app builder.

    The advantage over pubsub is that message channels aren’t restricted to a single page. The Publish-Subscribe pattern in Lightning Web Components is similar to the App-type event in Aura packages.

    Fire Event in One Component:

    import {LightningElement,api,wire} from 'lwc';
    import pubsub from 'c/pubsub' ;
    import { currentPageReference } from 'lightning/navigation';
    export default class LWCcommunity extends LightningElement {
    @wire(currentPageReference) pageRef;
    @api selectedMenu;
    sendMessage(){
    window.console.log('Event Firing.... ');
    let message = {
      "message" : 'Hello PubSub'
    }
    pubsub.fire('simplevt', message);
    window.console.log('Event Fired')
    }
    }

    dont miss out iconCheck out another amazing blog by Rahul here: All You Need to Know About Salesforce Governor Limits

    Registering event in Another Component:

    message;
    connectedCallBack(){
    this.register();
    }
    register(){
    window.console.log('event Registered');
    punsub.register('simplevt', this.handleEvent.bind(this));
    }
    handleEvent(messageFromEvt){
    window.console.log('event handled', messageFromEvt);
    this.message = messageFromEvt ? JSON.stringify(messageFromEvt, null, '\t'): 'no message payload';
    }