Introduction:
Imagine you have a customer who is not familiar with Salesforce and there is a need to add/update picklist values along with record types in the system, then you cannot expect him to follow the ideal steps (Setup->Object Manager->) to make the changes. So, this problem can be solved by using Lightning Component with Metadata API Service. This component enables a user to make the required changes  across multiple fields in your Salesforce instance with couple of clicks from the UI itself.
Unique features:
1. On click Create/Update picklist values.
2. On click assigning of picklist values to the record types.
3. On click removing of picklist values from the record types.
4. On click Update of records upon updation of picklist values.
Display UI:
Functionality:
Step1: Object Name – Displays all the object names present in your Salesforce instance
Select an Object
Step2: Picklist – Displays all the fields of the selected Object
Select a Picklist
Step3: Action – Displays two actions available Create/Update
Select an Action which you want to perform
Step3a: If Action selected is ‘Create’
Select Record Types: – Displays all the available record types
Select the Record Types to which you want to assign the new picklist value
Step3b: Description – Contains the name of the new picklist value
Give the name of the new picklist value here
Step4: Action – Displays two actions available Create/Update
Select an Action which you want to perform
Step4a: Picklist Value to Update – Displays the picklist values related to the picklist field selected
Select a picklist value which you want to update
Step4b: Select RecordTypes
Selected Options – Displays all the record type names in which the selected value is already present
Available Options – Displays all the record type names in which the selected value is not present
Select the record types to which the picklist value need to be assigned and remove the record types from which the picklist value need to be removed
Step4c: Description – Contains the updated name of the picklist value
Step5: Save
Clicking on
required operation will be performed.
UI:
UniversalPicklist.cmp
You have to build a lightning component to get UI as shown in the above snippet, you need to display five fields and a button.
Object Name: You can either hard code what all objects you want to display in this field or get all the objects using schema
Picklist: As soon as you select an Object Name you need to make a server call and get all the respective picklist fields available. You can get all the fields using schema
Action: Since we need only two different actions here, you can display those two values using <lightning:select>
- Create:- If you select ‘Create’ you need to make another server call and get all the record types in that particular object and display them in a format shown in the above snippet using <lightning:dualListbox>
- Update:- If you select ‘Update’ this means that you want to update an existing picklist value. So, here also you need to make a server call get all the picklist values for a selected picklist field. You can refer to this doc for getting the picklist values
Picklist Value to Update: The obtained picklist values will be displayed here. As soon as a picklist value is selected, you should get all the record types which has and which doesn’t have this picklist value and display on the UI accordingly, as shown in the snippet.
Description: This field holds the new or updated picklist value which user enters
Back end Logic:
MetadataApiService.apxc
You should have a MetadataApiService class.
Refer this link, you can use the same class.
UniversalPicklistController.apxc
Create/Update picklist value for custom fields: You can refer to this link for creating or updating the piclist values using metadataservices
Create/Update picklist value for standard fields: Refer the below code for standard fields:
//Read MetadataApiService.StandardValueSet StandardField = (MetadataApiService.StandardValueSet) service.readMetadata('StandardValueSet', new String[] { picklistapiname }).getRecords()[0]; MetadataApiService.StandardValue sv = new MetadataApiService.StandardValue(); sv.fullName = valueInTheDescription; sv.default_x = false; StandardField.standardValue.add(sv); // Update List<MetadataApiService.SaveResult> lstResults = service.updateMetadata( new MetadataApiService.Metadata[] { StandardField });
Updating Record Types: You can refer to this link to update Record Types
UniversalPicklistCntrlSessionIdGenerator.vfp
Use this code for generating session Id
<apex:page > Start_Of_Session_Id{!$Api.Session_ID}End_Of_Session_Id </apex:page>
UniversalPicklistControllerUtils.apxc
global class UniversalPicklistControllerUtils { global static String getSessionIdFromVFPage(PageReference visualforcePage){ String content= visualforcePage.getContent().toString(); Integer s = content.indexOf('Start_Of_Session_Id') + 'Start_Of_Session_Id'.length(), e = content.indexOf('End_Of_Session_Id'); return userinfo.getSessionId(); } }