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
devrequirementdevrequirement 

Deployment Error: FIELD_CUSTOM_VALIDATION_EXCEPTION

Following is the test class which garnered 91% test coverage in the sandbox for a class which was doing it's job already. However when I tried to validate the class through change sets while deploying it, the process was throwing me an error of FIELD_CUSTOM_VALIDATION_EXCEPTION at the line insert conobj2;

Stuffs I tried were: Setting up a user with a system admin profile to be associated in the test class as running user, create a particular record type and associate it with the process while confirming running user(which was me)'s profile has option to access the record type of the object, create a campaign then associate it with the contacts and accounts just to mimik the process. However, i was constantly being thrown the error.

Surprisingly when I commented out the conobj2, the validation and deployment was successful. However, could you please provide me some insight on how to correct the code in order to follow what it is supposed to do in the first place.
 Thanks in advance!!!!

@isTest(SeeAllData =true)

    private class EmailOptOutBatchTest{
    
    static testmethod void testEmailOptOutBatch() {
        Account accObj = new Account();
        accObj.Name='EmailOptOutTestUser';
        accObj.Num_of_Users__c=5;
        accObj.Type='Customer';
        accObj.Location_Type__c='Closed';
        accObj.AccountNumber='12345';
        accObj.BillingCountry='Nepal';
        accObj.Active__c = true;
       
        insert accObj;
        
       
        
        Contact ConObj1 = new Contact();
        ConObj1.Active__c = true ;       
        ConObj1.AccountId = AccObj.Id;
        ConObj1.FirstName = 'Paul';
        ConObj1.LastName = 'McCartney';
        ConObj1.Email='PM@p.com';
        ConObj1.Function__c = 'Unknown';
        ConObj1.HasOptedOutOfEmail=False;
        
        insert conObj1;
        
        Contact ConObj2 = new Contact();
      
        ConObj2.Active__c = true;
        ConObj2.AccountId = AccObj.Id;
        ConObj2.FirstName = 'John';
        ConObj2.LastName = 'Lennon';
        ConObj2.Email='PM@p.com';
        ConObj2.Function__c = 'Unknown';
        ConObj2.HasOptedOutOfEmail= True;
        //insert conObj2;
     
        
        if( Test.isRunningTest()) {
        ConObj1.HasOptedOutOfEmail = ConObj2.HasOptedOutOfEmail;
        }
        
        List<Contact> ContactListall = new List<Contact>();
        ContactListall.add(conObj1);
        ContactListall.add(conObj2);
       
 
         Test.startTest();
         
         EmailOptOutBatch objEmailOptOutBatch = new EmailOptOutBatch(true);
         ID batchprocessid = Database.executeBatch(objEmailOptOutBatch);
        
         Test.stopTest();
          }
}

Jeff MayJeff May

It looks like the only difference in the records is the value of 'HasOptedOut' field.  Does your target org have rules in place around 'True' -- for instance, does another field have to be populated like 'Reason'?