+ Start a Discussion

Test Methods BUG: Validation Rules

If we are not using the seealldata = true and thus have to create ALL records then why does the validation rules that fire during test methods see all data?


We have a few validation rule that check to ensure a field is unique. One of them being email.


In test methods I use a lot of test@tes.com for email addresses and sometimes users will create records with test@test.com. When the test methods are run after that they fail the validation rule.


For now, I will have to create a more uniwue email that users are not likely to use.


Anyone else observed this?



Ankit AroraAnkit Arora

This is interesting, I'll try this.


I hope you are not using the same email address in multiple records in single test execution. Or I think if run all is done it counts the previously created record in test method in same execution (please correct me if am wrong)


I also faced another strange problem with test classes. Let's say an object is having autonumber and in test class I have created three records (AN-00001, AN-00002, AN-00003), now when I create the original record it gets the count AN-00004.



Ankit Arora

Blog | Facebook | Blog Page



It is my understanding that autonumbers are consumed by test methods and they are not rolled back.


Oh, I am only inserting this one record with that email in this test method. 


Here is the debug log from my first post. The debug was placed immediatly before the insert. seealldata is NOT set. An existing record in the database before the test contains the email on the record of test@test.com. I am inserting a record during the test method using this email and there is a validation rule (using vlookup) to check for duplicates. (Behind the scenes, a trigger is parsing the domain from the email and the domain ends up being test.com and is the name of the record that cannot be duplicate.


21:08:42.742 (6742992000)|SOQL_EXECUTE_BEGIN|[31]|Aggregations:0|select ID, email from Contact where email = 'test@test.com'
21:08:42.753 (6753424000)|SOQL_EXECUTE_END|[31]|Rows:0
21:08:42.753 (6753480000)|USER_DEBUG|[31]|DEBUG|()
21:08:42.753 (6753614000)|DML_BEGIN|[35]|Op:Insert|Type:Contact|Rows:1



21:08:42.842 (6842952000)|CODE_UNIT_FINISHED|DE_ContactTrigger on Contact trigger event BeforeUpdate for [003Q000000ZoZ0Y]
21:08:42.869 (6869532000)|CODE_UNIT_STARTED|[EXTERNAL]|01qQ00000000ap1|ValidateDomain on Contact trigger event AfterUpdate for [003Q000000ZoZ0Y]
21:08:42.869 (6869655000)|SYSTEM_CONSTRUCTOR_ENTRY|[13]|<init>()
21:08:42.869 (6869666000)|SYSTEM_CONSTRUCTOR_EXIT|[13]|<init>()
21:08:42.869 (6869852000)|SOQL_EXECUTE_BEGIN|[14]|Aggregations:0|select ID, Name from RecordType where (sObjectType = 'Contact' and Name = 'US: Contact')
21:08:42.871 (6871287000)|SOQL_EXECUTE_END|[14]|Rows:1

**Notice this line - Shows no records returned**

21:08:42.871 (6871928000)|SOQL_EXECUTE_BEGIN|[80]|Aggregations:0|select Name from sbx_AuthorizedAccountDomain__c 
21:08:42.874 (6874784000)|SOQL_EXECUTE_END|[80]|Rows:0

21:08:42.874 (6874949000)|SYSTEM_CONSTRUCTOR_ENTRY|[85]|<init>()
21:08:42.874 (6874959000)|SYSTEM_CONSTRUCTOR_EXIT|[85]|<init>()
21:08:42.875 (6875274000)|USER_DEBUG|[99]|ERROR|***Insert: (sbx_AuthorizedAccountDomain__c:{Name=test.com, Account__c=001Q000000ZBgeGIAT})
21:08:42.875 (6875349000)|DML_BEGIN|[103]|Op:Insert|Type:sbx_AuthorizedAccountDomain__c|Rows:1
21:08:42.893 (6893842000)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:sbx_AuthorizedAccountDomain:new
21:08:42.893 (6893856000)|VALIDATION_RULE|03dQ00000000b8a|Require_Unique_Authorized_Domain_Name

**The validation that fails - Even though the database line above shows no records exist**

21:08:42.894 (6894149000)|VALIDATION_FORMULA|Name = VLOOKUP( $ObjectType.sbx_AuthorizedAccountDomain__c.Fields.Name , $ObjectType.sbx_AuthorizedAccountDomain__c.Fields.Name , Name ) && Id <> VLOOKUP( $ObjectType.sbx_AuthorizedAccountDomain__c.Fields.Id , $ObjectType.sbx_AuthorizedAccountDomain__c.Fields.Name , Name )|Name=test.com , $ObjectType.sbx_AuthorizedAccountDomain__c.Fields.Id=sbx_AuthorizedAccountDomain__c.Id , Id=null , $ObjectType.sbx_AuthorizedAccountDomain__c.Fields.Name=sbx_AuthorizedAccountDomain__c.Name
21:08:42.894 (6894168000)|VALIDATION_FAIL
21:08:42.894 (6894759000)|CODE_UNIT_FINISHED|Validation:sbx_AuthorizedAccountDomain:new
21:08:42.896 (6896650000)|DML_END|[103]
21:08:42.897 (6897778000)|VF_PAGE_MESSAGE|There was an error inserting the Domain. The error returned was: This domain name is already used for another Account.  Please use a unique domain name for this Account.
21:08:42.897 (6897845000)|USER_DEBUG|[112]|ERROR|**TRIGGER CHECK Delete: {}
21:08:38.902 (6897889000)|CUMULATIVE_LIMIT_USAGE





Did you ever get a chance to test this yourself?