You need to sign in to do that
Don't have an account?
Paul 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;
}
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;
}
Let us know if this will help you
All Answers
Let us know if this will help you
hey paul
am still struggling with this method for a test class.
it would be really helpful if you share your code.
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');
}
}
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;
}