Created
July 8, 2022 22:42
-
-
Save sriniind19/52332b49357b1b62b38cfb42a64357f7 to your computer and use it in GitHub Desktop.
Creating the dependency between Custom Picklist and Global Picklist using the MetadataService in Apex Salesforce
This file contains 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 static void createPicklistDependencyWithGlobalPicklist() { | |
// Creating each controlling value and the dependent value - STARTS | |
MetadataService.valueSettings controllingAndDependentValueSetting = new MetadataService.valueSettings(); | |
controllingAndDependentValueSetting.controllingFieldValue = new List<String>{ | |
'None' | |
}; | |
controllingAndDependentValueSetting.valueName = 'test1'; | |
List<MetadataService.valueSettings> controllingAndDependentValueSettings = new List<MetadataService.valueSettings>{ | |
controllingAndDependentValueSetting | |
}; | |
// Creating each controlling value and the dependent value - ENDS | |
// Valuset definition - STARTS | |
// pick list value | |
metadataservice.CustomValue firstPicklistValue = new metadataservice.CustomValue(); | |
firstPicklistValue.fullName = 'None'; | |
firstPicklistValue.label = 'None'; | |
firstPicklistValue.default_x = false; | |
MetadataService.ValueSetValuesDefinition valueSetDefinition = new MetadataService.ValueSetValuesDefinition(); | |
valueSetDefinition.sorted = false; | |
valueSetDefinition.value = new List<metadataservice.CustomValue>{ | |
firstPicklistValue | |
}; | |
// Valuset definition - ENDS | |
// Picklist ValueSet - STARTS | |
MetadataService.ValueSet picklistValueSet = new MetadataService.ValueSet(); | |
picklistValueSet.valueSetName = 'Remove'; // This is the global value set name | |
picklistValueSet.valueSettings = controllingAndDependentValueSettings; | |
picklistValueSet.controllingField = 'Remove_Picklist__c'; // Controlling Field including the object api name (for the global picklist this should not be there) | |
picklistValueSet.restricted = true; | |
// picklistValueSet.valueSetDefinition = valueSetDefinition; // This should not be added for the global picklst | |
// Picklist ValueSet - ENDS | |
// Custom Field - STARTS | |
MetadataService.CustomField customField = new MetadataService.CustomField(); | |
customField.fullName = 'Account.Global_Remove_Picklist__c'; // Dependent Field including the object name | |
customField.type_x = 'Picklist'; | |
customField.label = 'Global Remove Picklist'; | |
customField.valueSet = picklistValueSet; | |
// Custom Field - ENDS | |
// Deploying Custom Field - STARTS | |
MetadataService.MetadataPort service = createService(); | |
List<MetadataService.SaveResult> saveResult = new List<MetadataService.SaveResult> { | |
service.updateMetadata( | |
new MetadataService.Metadata[] { customField } | |
)[0] | |
}; | |
// Update Custom Field | |
handleSaveResults( | |
saveResult | |
); | |
// Deploying Custom Field - ENDS | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment