Search This Blog

Monday, 23 September 2019

Salesforce Dynamic Dependent Picklist


Demo



1.    Requirement

  • Build dynamic dependent picklist setup in salesforce system which can help to setup multiple level depend picklist setup from salesforce. 
  • System Administration and Standard should also add/remove picklist values along with set up dependencies on picklist metadata.


2.    Solution

 


2.1.  Solution Design


  • Custom objects to maintain picklist values and dependencies 
  • Metadata API to create picklist values and dependencie 
  • Automation to start Picklist value sync from custom object to picklist metadata using metadata API.

2.2.  Challenges

  • Metadata API to Crete Picklist values along with Dependencies
  •  Manage active/inactive picklist values
  •  Add Picklist values to Record Types
  •  Time Limit Issues due to Metadata Read Time Out
  •  Metadata API cannot access/execute by standard user which is salesforce limitations.


2.3.  Detailed Solution along with Example


Example: Setup below multi-level dynamic dependent picklist on Case object which can be manage by salesforce administration and standard users.
              Picklist 1 - Name: Country
              Picklist 2 - Name: State
              Picklist 3 - Name: City
             

2.3.1.      Setup Custom Objects


*  Create Main Custom Objects

o   Country Values

#  Name

#  Sort Order

o   State Values

#  Name

#  Sort Order

o   City Values

#  Name

#  Sort Order

*  Create Custom Object for Dependencies

o   Country and State - Junction Object between Country and State

#  Name: Auto Number

Country: Master Detail of Country Value

#  State: Master Detail of State Value

o   State and City - Junction Object between State and City

#  Name: Auto Number

#  State: Master Detail of City Value

#  City: Master Detail of State Value

*  Create Custom Object for Syncing

o   Sync Picklist Values

#  Picklist API Name: Text (It will be API Name of Picklist which are on Account)

#  Syncing State: Picklist (Start Syncing, In Progress, Completed, Failed)


2.3.2.      Create Metadata API class



Create Apex Class for Metadata Service Class from Gitub.
  

2.3.3.      Create Main Apex Class to Sync Picklist Values


             
o   Create Email Service apex class which will be having main logic of syncing Picklist Values, Build Picklist Dependencies and Add Picklist Values to Record Type
o   Due to Standard users cannot access/execute Metadata API, Email Service class is a solution where we can set context user (as system administration).
o   Create future method to sync each picklist sync, For Example:
#  syncCountry, along with parameter of Record Id (Sync Picklist Values object Record Id)
#  syncState, along with parameter of Record Id (Sync Picklist Values object Record Id)
#  syncCity, along with parameter of Record Id (Sync Picklist Values object Record Id)
o   Parameter of Sync Picklist Values object Record Id will help to track status of Syncing (Completed/Failed) along with Error Message.


2.3.3.1.           Pseudo Code for Sync Parent Picklist


Below is pseudo code to sync parent picklist values which will not need to sync picklist dependencies.
·      Update “Sync Picklist Value” Record Status to “Start Syncing”
·       Query all Records from “Country Values” custom object where all country values are created along with order by sort order.
·       Create list of MetadataService.CustomValue from all “Country Values” records.
·       Create generic method to pass that list of Custom Values and other parameters like Field API Name, Label etc. to Update Metadata Custom Field.
·       Build logic to Update Metadata Custom Field along with Picklist Values with Value Set object.
·       Update Metadata Custom Field (Country field of Account Object) using Metadata API
·       Create another generic method to Add picklist values to Record Type
·       Query Country object records where Last Modified Date is after Last Sync Date of “Sync Picklist Value” record.
·       Build logic to create list of “MetadataService.RecordType” and set list of Picklist values metadata.
·       Update list of “MetadataService.RecordType” using Metadata API


2.3.3.2.            Pseudo Code for Sync Child Picklist


Below is pseudo code to sync parent picklist values which will need to sync picklist dependencies.

·       Update “Sync Picklist Value” Record Status to “Start Syncing”
·       Query all Records from “State Values” custom object where all country values are created along with order by sort order.
·       Create list of MetadataService.CustomValue from all “State Values” records.
·       For Dependencies, Query all junction object records of “Country and State” object. Prepare map of dependent picklist value vs list of controlling values.
·       Based on that Map of Dependent and Controlling values, prepare a list of “MetadataService.ValueSettings” which will have controlling field values.
·       Create generic method to pass that list of Custom Values and other parameters like Field API Name, Label etc. to Update Metadata Custom Field. Here it will need to pass Value Settings (MetadataService.ValueSettings) for generate dependencies.
·       Rest of logic will be same as Parent Picklist Value syncing method.




2.3.4.      Email Service


·       Create Email Service with Email Service Address
·       Setup Email Service Apex Class which is built for Picklist Syncing
·       Assign System Administrator as Context User



2.3.5.      How to Start Syncing

  • Create Sync Picklist Value record for Each Picklist
  • Build Apex Trigger on “Sync Picklist Value” trigger
  • Trigger will execute while “Sync Picklist value” status will be updated to “Start Syncing”.
              Pseudo code of Trigger
  • Send an Email using Email Message to Email Service Address which is created.
  • Email will have Subject and Email body based. Email Subject/Body can have Picklist Name Identifier based on that identifier Email Service Class will execute the appropriate picklist syncing method. (syncCountry/syncState/syncCity). 
  • Also you can build subject/body to send Sync Picklist Value Record Id as identifier, so email service class can identify that which Record needs to update with Syncing Status.

THANK YOU


1 comment:

  1. Latest Certification Dumps of Salesforce , Amazon , IBM , Microsoft, SAP, Oracle , VMware available at https://www.ebsose.com or mail us at ebsosedotcom@gmail.com or certificationdirectdumps@gmail.com for any dumps / Superbadge /Development project queries . Get Fast replies and our team is available 24/7 . Success rate is 99.99% , so hurry and ping us .
    Reach out at ebsosedotcom@gmail.com for any development project or superbadge help required in salesforce or any other technology .

    ReplyDelete