0

I have to insert a new record that has a field with of type Picklist (Multi Select), and I get this error trying to insert:

TestValue is a value already added in the values list.

bad value for restricted picklist field: TestValue
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, bad value for restricted picklist field: TestValue: [Categories__c]
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, bad value for restricted picklist field: TestValue: [Categories__c]

The field data type in IDE "Categories__c" is showing String.

I have to run it in a Test Class (@IsTest)

I also found:

Product2.Categories__c.getDescribe().getPicklistValues();

Don't know how I can use it tho.

Thanks in advance.

  • Check, if recordtype exists and if you have assigned any default value in the picklist – Santanu Boral Aug 29 '18 at 19:32
  • Also check if the picklist has TestValue as an inactive value maybe ? – Rao Aug 29 '18 at 19:34
2

Mac, looks like you're trying to insert the value TestValue into a field of type picklist. The error really means this:

"TestValue" is not an option available on your picklist.

The easiest thing to do is to just choose a correct value. The method you found will help like this

List<Schema.PicklistEntry> values = Product2.Categories__c.getDescribe().getPicklistValues();
String myValue = '';
if (! values.isEmpty() ) { //this should not happen, but...
  myValue = values[0].getValue();
}
<YourObject>.Categories__c = myValue

If #1 didn't work, make sure that the recordtype your product2 record is under will accept the value you selected. This is something configurable via the GUI

PS: you can always just add "TestValue" to the list of available values in the picklist... but that's likely a horrible idea and you should never add a test value to a prod picklist just to make your tests pass. I am only providing this option here because perhaps your code does not use "TestValue" and instead uses a value that actually should show up in the picklist.

  • I actually did try setting the first value from that array, still the same problem... – Mac A. Aug 29 '18 at 22:49
  • Feel free to update your code with your latest efforts, so we can help you better – Sebastian Kessel Aug 29 '18 at 22:50

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.