Last active
July 19, 2022 13:16
-
-
Save xjsender/7c3cefbce563b19d4176f3a78d639bae to your computer and use it in GitHub Desktop.
Create Picklist Dependency by APEX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public with sharing class UpdatePicklistDependency { | |
| // A1 is the dependent value, A is the controlling value | |
| // which means A1 is controller by A, 1 => Many | |
| private static Map<String, List<String>> relationships = new Map<String, List<String>> { | |
| 'A1' => new List<String>{'A'}, | |
| 'A2' => new List<String>{'A'}, | |
| 'B1' => new List<String>{'B'}, | |
| 'B2' => new List<String>{'B'}, | |
| 'C1' => new List<String>{'C'}, | |
| 'C2' => new List<String>{'C'}, | |
| 'C3' => new List<String>{'C'} | |
| }; | |
| // Prepare the Org to call the Metadata API on the current user permissions | |
| public static MetadataService.MetadataPort createService(){ | |
| MetadataService.MetadataPort service = new MetadataService.MetadataPort(); | |
| service.SessionHeader = new MetadataService.SessionHeader_element(); | |
| service.SessionHeader.sessionId = UserInfo.getSessionId(); | |
| return service; | |
| } | |
| public static MetadataService.SaveResult[] updateDependency() { | |
| MetadataService.MetadataPort service = createService(); | |
| MetadataService.SaveResult[] results = service.updateMetadata( | |
| new List<MetadataService.Metadata> { | |
| getControllingField() | |
| } | |
| ); | |
| results = service.updateMetadata( | |
| new List<MetadataService.Metadata> { | |
| getDependentField() | |
| } | |
| ); | |
| return results; | |
| } | |
| private static MetadataService.CustomField getControllingField() { | |
| MetadataService.CustomField customField = new MetadataService.CustomField(); | |
| customField.fullName = 'Expense__c.Controller__c'; | |
| customField.label = 'Controller'; | |
| customField.type_x = 'Picklist'; | |
| /* Prepare the picklist with Controlling pickList field */ | |
| MetadataService.Picklist pt = new metadataservice.Picklist(); | |
| pt.sorted = true; | |
| pt.picklistValues = new list<metadataservice.PicklistValue>(); | |
| Set<String> values = new Set<String>(); | |
| for (List<String> vals : relationships.values()) { | |
| for (String val : vals) { | |
| values.add(val); | |
| } | |
| } | |
| for(String key : values){ | |
| MetadataService.PicklistValue plValue = new MetadataService.PicklistValue(); | |
| plValue.fullName = key; // Dependent picklist value | |
| plValue.isActive = true; | |
| plValue.default_x = false; // Set default to false | |
| pt.picklistValues.add(plValue); | |
| } | |
| customField.picklist = pt; | |
| return customField; | |
| } | |
| private static MetadataService.CustomField getDependentField() { | |
| MetadataService.CustomField customField = new MetadataService.CustomField(); | |
| customField.fullName = 'Expense__c.Dependent__c'; | |
| customField.label = 'Dependent'; | |
| customField.type_x = 'Picklist'; | |
| /* Prepare the picklist with Controlling pickList field */ | |
| MetadataService.Picklist pt = new metadataservice.Picklist(); | |
| pt.sorted= true; | |
| pt.controllingField = 'Controller__c'; | |
| pt.picklistValues = new list<metadataservice.PicklistValue>(); | |
| for(String key : relationships.keySet()){ | |
| MetadataService.PicklistValue plValue = new MetadataService.PicklistValue(); | |
| plValue.fullName=key; // Dependent picklist value | |
| plValue.isActive = true; | |
| plValue.default_x=false; // Set default to false | |
| plValue.controllingFieldValues = relationships.get(key); | |
| pt.picklistValues.add(plValue); | |
| } | |
| customField.picklist = pt; | |
| return customField; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't work now. There's no field called "picklist" on CustomField.