You need to sign in to do that
Don't have an account?
Lakshya Kanchan
Best approach to update picklist values using apex
Scenario:
1. I have a picklist field (User_List__c) on Account which displays the active users names as picklist values.
2. I want to update the list of active users in the User_List__c picklist field whenever a user is inserted or, when the user updates the name.
3. I have implemented a trigger on User to trigger the changes as per the above conditions.
4. But I am having issues in updating the picklist values.
NOTE :
- I have to update a single picklist value at a time and not all the values when updating the picklist value list.
1. I have a picklist field (User_List__c) on Account which displays the active users names as picklist values.
2. I want to update the list of active users in the User_List__c picklist field whenever a user is inserted or, when the user updates the name.
3. I have implemented a trigger on User to trigger the changes as per the above conditions.
4. But I am having issues in updating the picklist values.
NOTE :
- I have to update a single picklist value at a time and not all the values when updating the picklist value list.
Kindly attach your code over here,
As I understand your scenario you want to maintain collection of users who are in active status is I am right?
I figured out 2 ways:
1. Using tooling API
2. Using MetadataService class
I have 2 queries now wrt the requirement:
Query 1:
Which is a better way to implement & why?
Query 2:
- If I am using MetadataService class to add new values, the already present values get deactivated.
- I dont want the other values to be impacted while inserting a new value.
- The picklist contains "Admin" as a value by default, the label of which I am trying to update using metadata.
Code Snippet:
public class PicklistOperations {
//Code for Update
public static void updatePicklist(){
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = 'Account.Active_Users_List__c';
customField.label = 'Active Users List';
customField.type_x = 'Picklist';
// Define the Value Set and Value Set Definitions
MetadataService.ValueSet vs = new MetadataService.ValueSet();
MetadataService.ValueSetValuesDefinition vd = new MetadataService.ValueSetValuesDefinition();
// Define the picklist values
List<MetadataService.CustomValue> customValues = new List<MetadataService.CustomValue>();
MetadataService.CustomValue cv1 = new MetadataService.CustomValue();
cv1.label = 'Admin from Metadata 2';
cv1.fullName = 'Admin';
cv1.isActive = true;
cv1.default_x = false;
customValues.add(cv1);
vd.value = customValues;
vd.sorted = true;
vs.valueSetDefinition = vd;
// Use valueSet instead of Picklist
customField.valueSet = vs;
List<MetadataService.UpsertResult> results = service.upsertMetadata(new MetadataService.Metadata[] { customField });
}
}
Snippet to call the method : PicklistOperations.updatePicklist()