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
newbiewithapexnewbiewithapex 

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

How do I solve this error? If I select other picklist values form the list it gives me asserts error and if I select the mostly used values it give me the error above. I have googled the error and lots of people says uncheck the Restricted picklist checkbox but I will need that box checked in production so I was looking for some solution where I get to keep the box checked and not have the errors. 
@isTest
public with sharing class CloneListTest {

	static testmethod void testCloneListControllerOne() {
		
        List<Account> allAccounts = new List<Account>();
        
        //Create client Account
        Account clientAccount = new Account();
        clientAccount.Name = 'Client Account';
        
        //Create partner Accounts
        Account partnerAccountOne = new Account();
        partnerAccountOne.Name = 'Partner Account 1';
        
        Account partnerAccountTwo = new Account();
        partnerAccountTwo.Name = 'Partner Account 2';
        
        allAccounts.add(clientAccount);
        allAccounts.add(partnerAccountOne);
        allAccounts.add(partnerAccountTwo);
        
        insert allAccounts;
        
        Id clientAccountId = [SELECT Id from Account WHERE Id = :clientAccount.Id].Id;
        
        //Create parent Client Overview record (this will be deep cloned)
        Contract testClientOverview = new Contract();
        testClientOverview.AccountId = clientAccountId;
		testClientOverview.name = 'Client Overview Record';
        testClientOverview.RecordTypeId = System.Label.Program_Agreement_Record_Type;
            
		insert testClientOverview;
        
        Id clientOverviewId = [SELECT ID FROM Contract WHERE Id = :testClientOverview.id].Id;
		
        List<Contract> programOverviews = new List<Contract>();
        
        //Insert related Program Overview Records
        Contract testProgramOverviewOne = new Contract();
		testProgramOverviewOne.name = 'Program Overview Record 1';
        testProgramOverviewOne.AccountId = clientAccountId;
        testProgramOverviewOne.Program_Name__c = 'Diabetes';
        testProgramOverviewOne.Client_Overview__c = clientOverviewId;
        testProgramOverviewOne.RecordTypeId = System.Label.Program_Schedule_Record_Type;
        
        Contract testProgramOverviewTwo = new Contract();
		testProgramOverviewTwo.name = 'Client Overview Record';
        testProgramOverviewTwo.AccountId = clientAccountId;
        testProgramOverviewTwo.Program_Name__c = 'Hypertension';
        testProgramOverviewTwo.Client_Overview__c = clientOverviewId;
        testProgramOverviewTwo.RecordTypeId = System.Label.Program_Schedule_Record_Type;

        programOverviews.add(testProgramOverviewOne);
        programOverviews.add(testProgramOverviewTwo);
        
		insert programOverviews;
        
        //Create Contract-level Partnership Relationship records
        List<Relationship__c> allRelationships = new List<Relationship__c>();
        
        Relationship__c testRelationshipOne = new Relationship__c();
		testRelationshipOne.RelationshipType__C = 'Contract Path'; //Relationship Type should be "Contract Path"
        //testRelationshipOne.RelationshipType__C = 'Activity Tracking Devices'; //Relationship Type should be "Contract Path"
        testRelationshipOne.Partner_Contract_Partnership__c = [select Id FROM Account WHERE Id = :partnerAccountOne.Id].Id;
		testRelationshipOne.Contract_Partnership__c = clientOverviewId;
		allRelationships.add(testRelationshipOne);       
        
        Relationship__c testRelationshipTwo = new Relationship__c();
		testRelationshipTwo.RelationshipType__C = 'Billing Partner'; //Relationship Type should be "Billing Partner"
        //testRelationshipTwo.RelationshipType__C = 'Asthma'; //Relationship Type should be "Billing Partner"
        testRelationshipTwo.Partner_Contract_Partnership__c = [select Id FROM Account WHERE Id = :partnerAccountTwo.Id].Id;
		testRelationshipTwo.Contract_Partnership__c = clientOverviewId;
		allRelationships.add(testRelationshipTwo);
        
        insert allRelationships;

		Test.startTest();
        List<List<String>> testSourceInformation = new List<List<String>>();
        //Source Object Name, Child Object Name, Parent Id
        List<String> allInfo = new List<String>{'Contract', 'Contract, Relationship__c', string.valueof(clientOverviewId)};
        
            testSourceInformation.add(allInfo);
        
        List<String> clonedParentId = clientOverviewServices.copyChildRecords(testSourceInformation);
        Test.stopTest();
        Contract clonedParentRecord = [SELECT Name, Is_Cloned__c FROM Contract WHERE Id = :Id.valueOf(clonedParentId.get(0))];
        
        System.assertEquals(TRUE, clonedParentRecord.Is_Cloned__c);
        System.assertEquals(2, [SELECT Id FROM Contract WHERE Client_Overview__c = :Id.valueOf(clonedParentId.get(0))].size());
        System.assertEquals(2, [SELECT Id FROM Relationship__c WHERE RelationshipType__c = 'Contract Path'].size());
        System.assertEquals(2, [SELECT Id FROM Relationship__c WHERE RelationshipType__c = 'Billing Partner'].size());
        //System.assertNotEquals(null, clonedParentId);
		//System.debug(System.assertEquals(0, [select count() from Contact where AccountId =: testAccountTwo.Id]);
		
	}		
}
Best Answer chosen by newbiewithapex
Raj VakatiRaj Vakati
This error occurs when the picklist field mentioned in the error message meets the following conditions:
  • 'Restrict picklist to the values defined in the value set' is enabled on the mentioned picklist field.
  • The mentioned picklist field is a dependent picklist.
  • The controlling and dependent picklist fields are not included on the page layout.
  • A default value is set for the mentioned picklist field.
  • If the Object includes Record Types, this error will occur if the Record Type sets the default value
  • If the Object does not include Record Types, this error will occur if the picklist field sets the value
  • When users attempt to save a record in the object that contains this picklist field, the system will attempt to populate the default value. Since the controlling value is not set, and the picklist is restricted to only allow the dependent values, the attempt to save the record returns an error.
https://help.salesforce.com/articleView?id=000250168&language=en_US&type=1


You can fix in two ways 
  1. Remove Restriction on the picklist 
  2. or pass the correct picklist vale