function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Øyvind Borgersen 10Øyvind Borgersen 10 

Help - Test class for picklist entries and labels


I've written a code to get picklistvalues instead of api names and pass that value back. Can someone assist in a test class for the following code:

public without sharing class OneConsent {
    //Method to be used in flow to pass Id to class -- Return string name
    @InvocableMethod(label='Get Contact Citizenship name' description='Return the citizenship label')
    public static List<String> getContactId (List<ID> ids) {
      List<String> ContactId = new List<String>();
      List<String> CitizenshipLabel = new List<String>();
    // Use the describe information to generate a mapping from api name to label
    List<Schema.PicklistEntry> PicklistEntries = Contact.hed__Citizenship__c.getDescribe().getPicklistValues();
    map<String,String> ApiToLabel = new map<String,String>();
    for (Schema.PicklistEntry pe : PicklistEntries){
        // List contact values to get current citizenship value
        List<Contact> ContactValue = [SELECT hed__Citizenship__c FROM Contact WHERE Id in :ids];
        // Loop contact value and assign picklist api name to picklist value
        for(contact con: contactvalue) {
            system.debug('Label'+ ApiToLabel.get(con.hed__Citizenship__c));
            String str = string.valueOf(ApiToLabel.get(con.hed__Citizenship__c));
        return CitizenshipLabel;
AnudeepAnudeep (Salesforce Developers) 
Posting here a sample code to cover schema.picklistentry

public class PicklistEntryTest {
    public List<SelectOption> getAccountTypeOptionsList(){
        List<SelectOption> accountTypeOptionsList = new List<SelectOption>{new SelectOption('None', '--None--')};
            Schema.DescribeFieldResult accountTypeDescription = Account.Type.getDescribe();
        List<Schema.PicklistEntry> accountTypePicklistValuesList = accountTypeDescription.getPicklistValues();
        for(Schema.PicklistEntry accountTypePicklistValue : accountTypePicklistValuesList){
            accountTypeOptionsList.add(new SelectOption(accountTypePicklistValue.getLabel(), accountTypePicklistValue.getValue()));
        return accountTypeOptionsList;

Test Class
public class Test_PicklistEntryTest {

    static testMethod void testPicklist() {
        PicklistEntryTest pe = new PicklistEntryTest();

            List<SelectOption> options = pe.getAccountTypeOptionsList();
        system.assertEquals(options.get(0).getValue(), 'None');

Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you