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
arandallarandall 

Test Class Failing Due to Validation Rule

Hello,

 

I have a validation rule that requires the BillingState field to be populated for an Account to be created.  I'm trying to deploy a test class that creates test Account records, and when I validate the change set, it fails, indicating that the validation rule was set off.  The odd thing is that I'm populating the BillingState field of the test Account records in the test class, so I can't figure out what the validation rule is still being set off.  Any thoughts as to why this might be happening?

 

The error condition formula for my validation rule is ISBLANK(BillingState), and here's my test class:

 

@isTest
private class testSchoolImpLink {

static testMethod void myTest() {

//Insert Accounts
List<Account> accounts = new List<Account>();
for(Integer i=0; i<5; i++){
Account a = new Account(Name = 'Test Account' + i, BillingState = 'TX');
accounts.add(a);

}
insert accounts;

//Insert an Opportunity
Opportunity o = new Opportunity(Name = 'Test Opportunity', StageName = 'Lead', CloseDate = date.newInstance(2011, 5, 2));
insert o;

//Get accounts back
List<Account> new_accounts = [SELECT id FROM Account];
Opportunity opp = [SELECT id FROM Opportunity][0];

//Insert School Implementations
List<School_Implementation__c> school_imps = new List<School_Implementation__c>();
for(Integer i=0; i<5; i++){
School_Implementation__c s = new School_Implementation__c(Name = 'Test School Implementation', Enrollment_Opportunity__c = opp.id, School_Implementation_Status__c = 'Active', School__c = new_accounts[i].id);
school_imps.add(s);
}
//Start the test
test.startTest();

insert school_imps;

//Stop the test
test.stopTest();

//Query database for new Accounts
List<Account> newest_accounts = [SELECT id, Most_Recent_School_Implementation__c, BillingState FROM Account];

//For each account, make sure that Current School Implementation is correct
for(Account a : newest_accounts){
System.assert(a.Most_Recent_School_Implementation__c != null);
}
}
}

Best Answer chosen by Admin (Salesforce Developers) 
arandallarandall

Hi all,

 

Thanks for everyone's help.  It turned out that it wasn't my test class that was causing the validation rule to go off - it was another (similarly-named) test class that was causing it.  I've fixed that test class and have migrated this test class into production successfully.

 

Thanks again!

 

Alex

All Answers

RustanRustan

I am not sure if this is the problem but try replacing that line with this. Just go ahead and try it.

 

Account a = new Account(Name = 'Test Account' + string.valueof(i), BillingState = 'TX');

bouscalbouscal

Are you able to create an account in the GUI with only a name and the billing state?  Could be additional data validation firing and attempting to do this in the GUI will give you immediate and direct feedback if it fails.

arandallarandall

Tried it, no luck.  Thanks for the suggestion, though!

arandallarandall

Yes, I can create Accounts with only Name and Billing State.  Could it have anything to do with the fact that I'm creating them outside of the test.startTest method?

arandallarandall

Hi all,

 

Thanks for everyone's help.  It turned out that it wasn't my test class that was causing the validation rule to go off - it was another (similarly-named) test class that was causing it.  I've fixed that test class and have migrated this test class into production successfully.

 

Thanks again!

 

Alex

This was selected as the best answer