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
Jared RosenbergJared Rosenberg 

Can't test picklist value

Good morning folks!

I've got a custom object (Work Order Line) that has a restricted picklist field by the name of Reason Code (Reason_Code__c).  This field is validated as other objects move through different statuses, so in the process of creating unit tests I need to be able to assign a value for it.  Unfortunately, it's giving me a lot of trouble doing that.

The first available value of the picklist, and the one I'm trying to test with, has a value of '01 - Charged - per contract/entitlement' and api name of '01 - Charged - per contract/entitlement'

In a test class, I've tried setting it by giving it a string literal.  That errored with:

System.DmlException: Update failed. First exception on row 0 with id a238E000001AQxpQAG; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, bad value for restricted picklist field: 01 - Charged - per contract/entitlement: [Reason_Code__c]

In desperation, I've even fetched the picklist value from the object and fed it right back to it.  That fails with the same error.  Code snippet follows:

        SVMXC__Service_Order_Line__c orderLine1 = new SVMXC__Service_Order_Line__c();
        orderLine1.SVMXC__Service_Order__c = workOrder1.Id;
        orderLine1.Date_parts_are_required_by__c = Date.today();
        orderLine1.SVMXC__Requested_Quantity2__c = 10;
        orderLine1.Consumed_Qty__c = 3;
        orderLine1.Returned_Qty__c = 0;
        orderLine1.SVMXC__Product__c = objLineProd.id;
        insert orderLine1;
        List<String> reasonCodeLabels = new List<String>();
        Schema.DescribeFieldResult fieldResult = SVMXC__Service_Order_Line__c.Reason_Code__c.getDescribe();
        List<Schema.PicklistEntry> reasonCodes = fieldResult.getPicklistValues();
        for(Schema.PicklistEntry ple : reasonCodes) {
            reasonCodeLabels.add(ple.getValue());
        }
        String reason = reasonCodeLabels.get(0);
        orderLine1.Reason_Code__c = reason;
        update orderLine1;
        workOrder1.SVMXC__Order_Status__c= 'Ready to Invoice';
        update workOrder1;
        objCase.Status = 'Ready to Invoice';
        update objCase;
        Test.stopTest();

This errors on the line: orderLine1.Reason_Code__c = reason; with the same error above.

I can't figure out why it won't let me set the picklist to a value I just took from the picklist accepted values.  Can anyone assist with this?

Thanks!
Jared RosenbergJared Rosenberg
As a quick note, I can confirm that the picklist has no deactivated values.  It's not picking a value that's otherwise invalid.  '01 - Charged - per contract/entitlement' is definitely active and in regular use.
Prachi Goel 17Prachi Goel 17
Hi,
These can be Reasons for this Error
1. Either picklist values are not activated
2. Either These values are not added on Record type

Please mark it best answer if it helps!!!
Jared RosenbergJared Rosenberg
@Prachl Goel 17

Morning!

Unfortunately I'm quite sure that these picklist values are active, and are valid for the record type.  They're perfectly functional through the front end on this type of record.  I'm wondering if there's possibly some characters we've used that are invalid for strings being passed?  Other parts of my test script are referencing picklist values by string with no problem.