top of page
  • Writer's pictureA. Tikale

How to implement a custom dependency with two multi-select picklist values in Salesforce

Updated: Jun 22


Hey Learners! In this blog, I am bringing a good add-on to your knowledge. This blog is about implementing custom dependency logic between two multi-select picklist fields in Salesforce offers a dynamic and user-friendly way to guide users through a process or to ensure data integrity by presenting only relevant options based on previous selections. This approach is particularly useful in scenarios where standard picklist dependencies do not suffice, such as when both fields are multi-select picklists or when complex logic determines the dependency using LWC


What is a Multi-Select Picklist? 

A multi-select picklist in Salesforce is a custom field that enables users to choose multiple values from a list of options. This field is quite useful when capturing various types of data where more than one selection is applicable.



Custom dependency

In Salesforce, a custom dependency is a feature that allows for a specific configuration where the available options in one field (known as the dependent field) change dynamically based on the value selected in another field (the controlling field). This functionality has been designed to ensure that the values presented in the dependent field are relevant to the selection made in the controlling field, which ultimately helps to streamline data entry and improve data quality.


Let’s see with the help of an example how to achieve this requirement. The requirement that I will be solving today is as follows: 


Example Scenario

You are working for a company that manages a library system using Salesforce. The company wants to categorize books based on multiple genres and sub-genres.

  • Genre (Picklist1): This multi-select picklist includes various genres like Fiction, Non-Fiction, Science Fiction, Fantasy, etc.

  • Sub-Genre (Picklist2): This multi-select picklist includes sub-genres that depend on the selected genres. For example, if Fiction is selected, sub-genres like Mystery, Romance, and Thriller should be available. If Non-Fiction is selected, sub-genres like Biography, History, and Self-Help should be available.


Step 1: Create Multi-Select Picklist Fields

  1. Create the Multi-Select Picklist Fields:

  • Go to Fields & Relationships.

  • Click New and select Multi-Select Picklist.

  • Define the first multi-select picklist Genre__c with values like Fiction, Non-Fiction, Science Fiction, and Fantasy.

  • Define the second multi-select picklist Sub_Genre__c with values like Mystery, Romance, Thriller, Biography, History, Self-Help, etc.


Step 2: Create the LWC Component


bookPicklist.html :

<template>
  <lightning-card title={label.PICKLISTS}>
    <div class="slds-p-around_medium">
      <lightning-dual-listbox name={label.GENRES} label={label.SELECT_GENRES}
        source-label={label.AVAILABLE_GENRES} selected-label={label.SELECT_GENRES}
        field-level-help={label.SELECT_ONE_OR_MORE_GENRES} options={genreOptions}
        value={selectedGenres} onchange={handleGenreChange}>
      </lightning-dual-listbox>
    </div>
    <div class="slds-p-around_medium">
      <lightning-dual-listbox name={label.SUBGENRES} label={label.SELECT_SUB_GENRES}
        source-label={label.AVAILABLE_SUB_GENRES} selected-label={label.SELECT_SUB_GENRES}
        field-level-help={label.SELECT_ONE_OR_MORE_SUB_GENRES} options={subGenreOptions}
        value={selectedSubGenres} onchange={handleSubGenreChange}>
      </lightning-dual-listbox>
    </div>
  </lightning-card>
</template>

bookPicklist.js : 

import {LightningElement, track} from 'lwc';
import PICKLISTS from '@salesforce/label/c.PICKLISTS';
import GENRES from '@salesforce/label/c.GENRES';
import SELECT_GENRES from '@salesforce/label/c.SELECT_GENRES';
import AVAILABLE_GENRES from '@salesforce/label/c.AVAILABLE_GENRES';
import SELECT_ONE_OR_MORE_GENRES from '@salesforce/label/c.SELECT_ONE_OR_MORE_GENRES';
import SUBGENRES from '@salesforce/label/c.SUBGENRES';
import SELECT_SUB_GENRES from '@salesforce/label/c.SELECT_SUB_GENRES';
import AVAILABLE_SUB_GENRES from '@salesforce/label/c.AVAILABLE_SUB_GENRES';
import SELECT_ONE_OR_MORE_SUB_GENRES from '@salesforce/label/c.SELECT_ONE_OR_MORE_SUB_GENRES';
export default class BookPicklist extends LightningElement {
  label = {
    PICKLISTS,
    GENRES,
    SELECT_GENRES,
    AVAILABLE_GENRES,
    SELECT_ONE_OR_MORE_GENRES,
    SUBGENRES,
    SELECT_SUB_GENRES,
    AVAILABLE_SUB_GENRES,
    SELECT_ONE_OR_MORE_SUB_GENRES
  };
@track genreOptions = [
        { label: 'Fiction', value: 'Fiction' },
        { label: 'Non-Fiction', value: 'NonFiction' },
        { label: 'Science Fiction', value: 'ScienceFiction' },
        { label: 'Fantasy', value: 'Fantasy' }
    ];
    @track selectedGenres = [];
    @track subGenreOptions = [];
    @track selectedSubGenres = [];


    handleGenreChange(event) {
        this.selectedGenres = event.detail.value;
        this.updateSubGenreOptions();
    }


    handleSubGenreChange(event) {
        this.selectedSubGenres = event.detail.value;
    }


    updateSubGenreOptions() {
        this.subGenreOptions = [];
        this.selectedGenres.forEach(genre => {
            if (genre === 'Fiction') {
                this.subGenreOptions = this.subGenreOptions.concat([
                    { label: 'Mystery', value: 'Mystery' },
                    { label: 'Romance', value: 'Romance' },
                    { label: 'Thriller', value: 'Thriller' }
                ]);
            }
            else if (genre === 'NonFiction') {
                this.subGenreOptions = this.subGenreOptions.concat([
                    { label: 'Biography', value: 'Biography' },
                    { label: 'History', value: 'History' },
                    { label: 'Self-Help', value: 'SelfHelp' }
                ]);
            }
        });
    }
}

Test your development

  • The ‘bookPicklist’ component will be added to any record page.

  • Sub-genres are empty before you choose any value


  • Choose any option from Genres. I chose Fiction. Depending on it Sub-genres will show values.


Benefits:

  • Enhanced Sales Process: By guiding sales associates towards relevant accessories, the solution can increase average order value through effective up-selling and cross-selling.

  • Improved Customer Experience: Customers benefit from a more tailored shopping experience, where relevant accessory options are presented based on their initial interest.

  • Data-Driven Insights: Over time, analyzing the relationships between product categories and accessory types that lead to sales can provide valuable insights into customer preferences and inventory management.


Advantage of Multiselect picklist 

  • Multi-select picklists let users choose multiple options without restrictions, making them ideal for capturing all applicable values in scenarios where more than one option can be relevant.

  • Instead of creating multiple checkboxes or related records for each option, a multi-select picklist consolidates choices into a single field, simplifying the form and saving screen space.


Disadvantages of Multiselect picklist 

  • Salesforce reports and SOQL queries have limitations when it comes to multi-select picklists, Up to 500 values and Up to 255 characters per value can be created.

  • Users can select up to 100 values at a time on a record.

  • Enforcing validation rules or ensuring dependencies between multi-select picklists and other fields is more complicated 


Conclusion

This solution provides a way to create a custom dependency between two multi-select picklists in Salesforce using Lightning Web Components (LWC). The LWC and JavaScript logic work together to dynamically update the options available in the second picklist based on the selections made in the first picklist.


If you'd like to see the code and resources used in this project, you can access the repository on GitHub.To access the AVENOIRBLOGS repository, click here. Feel free to explore the code and use it as a reference for your projects.


Thank You! You can leave a comment to help me understand how the blog helped you. If you need further assistance, please contact us. You can click "Reach Us" on the website and share the issue with me.


Reference



Blog Credit:

A. Tikale

Salesforce Developer

   Avenoir Technologies Pvt. Ltd.

  Reach us: team@avenoir.ai

Commenti


bottom of page