I have a record type picklist on Case object. When I create case via email-to-case that works fine without any issue(I have some APEX code related to that)

Issue: The problem is when I write test cases for that and run the test that says on insert and update.

DEBUG|Insert failed. First exception on row 0; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, bad value for restricted picklist field

What is the issue? Even though that works fine when I create case via email-to-case.


Case newCase = new Case();
newCase.Origin = 'Email';
newCase.Subject = 'Subject';
newCase.OwnerId = System.Label.Default_Case_Owner_Queue;
newCase.Status = 'New';
newCase.Priority = 'Medium';
newCase.Description = 'This is a new case';
newCase.Type = 'Invoicing';
newCase.Email__c  = '[email protected]';
newCase.Close_Code__c = 'test'; // this is the picklist which is causing issue, the picklist value was already added to record type.
insert newCase;

How can I fix that to pass the tests? TIA

  • Can you check API name for the picklist, sometime that can be different. And try to copy paste the API name and use it in the test block.
    – Ysr Shk
    Commented Sep 24, 2020 at 9:27
  • 1
    You may have to set the record type while insert. In your code we do not see the record type assignment.
    – Raul
    Commented Sep 24, 2020 at 9:30
  • How can I consider record type in the code? I assumed this is the record type code newCase.Type = 'Invoicing'; Commented Sep 24, 2020 at 9:40
  • So the record type was the issue. I added that and now it works fine. Please add as answer then I will accept the answer. Record type code adding by following this salesforce.stackexchange.com/a/141101/85098 Commented Sep 24, 2020 at 9:47

1 Answer 1


During the insert the record type assignment is missing and the default record type may not have the picklist value that you are trying to insert with, so add the record type assignment-

Case newCase = new Case();
newCase.RecordTypeId = '{get value from describe call}';
newCase.Close_Code__c = 'test';
insert newCase;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .