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
Paul 32Paul 32 

help writing test for t Schema.PicklistEntry

I need help writing a test that contains Schema.PicklistEntry I have looked everywhere and cant figure this out, i am new to Apex so any help would be greatly appreciated. below is the method, which returns all picklists and values on the specifed object.

   
    public static List <String> getselectOptions(sObject objObject, string fld) {
        system.debug('objObject --->' + objObject);
        system.debug('fld --->' + fld);
        List < String > allOpts = new list < String > ();

        Schema.sObjectType objType = objObject.getSObjectType();        
   
        Schema.DescribeSObjectResult objDescribe = objType.getDescribe();        
    
        map < String, Schema.SObjectField > fieldMap = objDescribe.fields.getMap();        
      
        list < Schema.PicklistEntry > values =
            fieldMap.get(fld).getDescribe().getPickListValues();        
     
        for (Schema.PicklistEntry a: values) {
            allOpts.add(a.getValue());
        }
        system.debug('allOpts ---->' + allOpts);
        allOpts.sort();
        return allOpts;
    } 
Best Answer chosen by Paul 32
Amit Chaudhary 8Amit Chaudhary 8
You simply need to wright test class like below
 
Account acc ;
List<String> lstString = CLASSNAME.getselectOptions(acc , 'Type');


Let us know if this will help you

All Answers

Amit Chaudhary 8Amit Chaudhary 8
You simply need to wright test class like below
 
Account acc ;
List<String> lstString = CLASSNAME.getselectOptions(acc , 'Type');


Let us know if this will help you
This was selected as the best answer
Paul 32Paul 32
Hey Amit It turns out I wasnt that far off, just made a small mistake, but your advice helped thanks for the help!
Shaik naina 5Shaik naina 5

hey paul

am still struggling with this method for a test class.

it would be really helpful if you share your code.

Paul 32Paul 32
Hey Shaik, the answer has been marked above but for clarity you need to add a line in your test method like CLASSNAME.getPiklistValues(); 
Shaik naina 5Shaik naina 5

hi paul 

this is my code can you give a solution or can you share your code?

 @isTest 
    public static void picklistValueTest() {
 prepareTestData();
        try
        {
            List<String> lstString = LightningCaseController.getselectOptions(testWorkorder , 'Up');           
        }
        catch(Exception err)
        {
            system.debug('error');
        }

    } 
 

Paul 32Paul 32
Hi Shaik what field are you trying to get the picklist values from?

If you want the vlues from one picklist try this:


        @AuraEnabled
        public static List <String> getPiklistValues(string obj, string fld) {
        List<String> plValues = new List<String>();
        
        //Get the object type from object name. .
        Schema.SObjectType objType = Schema.getGlobalDescribe().get(obj);
        
        //Describe the sObject using its object type.
        Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
        
        //Get the specific field information from field name. .
        Schema.DescribeFieldResult objFieldInfo = objDescribe.fields.getMap().get(fld).getDescribe();
        
        //Get the picklist field values.
        List<Schema.PicklistEntry> picklistvalues = objFieldInfo.getPicklistValues();
        
        //Add the picklist values to list.
        for(Schema.PicklistEntry plv: picklistvalues) {
            plValues.add(plv.getValue());
        }
        plValues.sort();
        system.debug('VALUES :'+plValues);
        return plValues;
    }


 
Shaik naina 5Shaik naina 5
yes im using this method only...but paul i need the test class for this...kindly share your test class if you dont mind 
Paul 32Paul 32
Hi Shaik, Your test works fine with the code sample i have put up, i got 100% coverage against my code using your test.