0

Added explanation: Since the Case.picklistField is constantly modified by other Org user (non-developer), so I want to avoid data dependency as much as I could.

I have a case trigger method using 'label from a Case.picklistField' to map the action, and now I come across a problem:

The test method I've written CANNOT set label to the Case.picklistField, only can set value to the Case.picklistField.

My goal is to add a new value/label set to the picklist field other then the now exist value/label set, which is only be used in test method.

Trigger method goes like this:

//Get AutoClose__c setting from CustomObj
Map<String, Boolean> AutoCloseFuction_M = new Map<String, Boolean>();
    for(CustomObj objCS : [Select Function_Labelname__c, AutoClose__c From CustomObj where AutoClose__c = true ])
    AutoCloseFuction_M.put(objCS.Function_Labelname__c, objCS.AutoClose__c);

//Get (value, label) from Case.PicklistField__c
Map<String,String> MapValueLabel = new Map<String,String>();
Schema.DescribeFieldResult fieldResult = Case.PicklistField__c.getDescribe();
List<Schema.PicklistEntry> values = fieldResult.getPicklistValues();
for( Schema.PicklistEntry v : values) {
    MapValueLabel.put(v.getValue(),v.getLabel());
}

//Main logic Start
for(Case C: triggerNewL)
{
    if( String.isNotBlank(C.PicklistField__c) )
    {
        if(
                ( AutoCloseFuction_M.containsKey( MapValueLabel.get(C.PicklistField__c) ) )&&
                ( C.Disable_AutoClose__c == false )
            )
        {
            C.Status = 'Close';
        }
    }
}
//Main logic End
  • 1
    If you insert value to picklist in test class it will check against the picklist entries defined in schema. You don't need to set label for picklist in test class. – Reshma Jan 18 at 8:15
  • Thanks for the info, but want I would like to achieve there is to "add a new value/label set to the picklist field which is only use in test method". Because i don't want to affect the Setting in Production environment. – 陳紅狼 Jan 19 at 3:19
0

To answer the main part of the question, no you cannot set a picklist label in a unit test.

But that doesn't mean you can't write a test to get the results you want. Currently you build up MapValueLabel with label-value pairs from the Schema, but it's still just a Map<String, String> and it's being used in the logic to determine whether or not the Case should be closed. You can add any pair of strings to the map that you want for the purposes of your test. If you replace your inline SOQL query with a data accessor class you could mock the results of the query rather than hitting the database in your org directly and your mock class can return CustomObj records with data that can make your test Cases close or stay open for the purposes of testing.

0

You will not be able to change the picklist value set in a unit test context. You'll have to structure your test differently.

This could take a couple of forms, but I think the clearest is to tailor the test record you create in CustomObj to the available picklist record set - that is, look at the describe results, pick any available picklist value and label, and create an auto-close record to match - and then do the same with your test Case.

Assuming your org doesn't also have validation against this rapidly-changing picklist field, which would make maintenance very difficult, that should allow you to dynamically create a testable situation regardless of the picklist's current value set.

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.