I want to add pick list value in test class i.e when i select pick list value it should be updated/added in the corresponding test class


It's not hard (Assuming the value to be passed to the test class is the default picklist value)..

Create a new Static Method which would retrieve the default picklist

Method 1: Apex Method

public static string retrieveDefaultPicklist(String sObjectName , String picklistFieldApi) {
    Schema.DescribeFieldResult fieldSchema = Schema.getGlobalDescribe().get(sObjectName).getDescribe().fields.getMap().get(picklistFieldApi).getDescribe();
    List<Schema.PicklistEntry> schemaList = fieldSchema.getPicklistValues();
    string defaultPicklistLabel;

    if (!schemaList.isEmpty()) {
        for( Schema.PicklistEntry schema : schemaList){
            valuesList.put(s.getValue(), s.getLabel());
            if (schema.isDefaultValue()) {
                defaultPicklistLabel = schema.getValue()
    return defaultPicklistLabel;

call this function when assigning the picklist value in the test class.

to retrieve values other than the default values the method can be updated

Method 2: Custom Label A custom label can also be created referring to the value and assigning it in apex test class (the value and then the value can be updated on a request basis in setup)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.