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

Hi, 

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){
        ApiToLabel.put(pe.getValue(),pe.getLabel());
    }    
    
        // 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));
            CitizenshipLabel.add(str);
        }
        
        return CitizenshipLabel;
         }
    }
AnudeepAnudeep (Salesforce Developers) 
Posting here a sample code to cover schema.picklistentry

Controller
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
 
@isTest
public class Test_PicklistEntryTest {

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

    	Test.startTest();
            List<SelectOption> options = pe.getAccountTypeOptionsList();
        Test.stopTest();
        
        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