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
frofrik2frofrik2 

Test case failing on validation rule that should pass

Hi,

I'm having problems with a test class that fails on a validation rule even when it shouldn't.
When running it, it's failing with the following reason: 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, mapOwnerInfoToAccount: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 0012000000e1kjCAAQ; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Store-ID can not be populated when Status is set to Prospecting. You need to choose the status Test Mode or Live.: [Status__c] Trigger.mapOwnerInfoToAccount: line 150, column 2: []


But in the test case, the Status__c is set to Live, so it shouldn't be a problem.
Though when I look through the log of the test case it seems to pass first and then it just fails, can't understand why.
Copied a part of the log:

11:51:59.049|WF_ACTIONS_END| None
11:51:59.049|CODE_UNIT_FINISHED|Workflow:Account
11:51:59.051|DML_END|[150]
11:51:59.051|VF_PAGE_MESSAGE|Store-ID can not be populated when Status is set to Prospecting. You need to choose the status Test Mode or Live.
11:51:59.052|EXCEPTION_THROWN|[150]|System.DmlException: Update failed. First exception on row 0 with id 0012000000e1kjCAAQ; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Store-ID can not be populated when Status is set to Prospecting. You need to choose the status Test Mode or Live.: [Status__c]
11:51:59.058|FATAL_ERROR|System.DmlException: Update failed. First exception on row 0 with id 0012000000e1kjCAAQ; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Store-ID can not be populated when Status is set to Prospecting. You need to choose the status Test Mode or Live.: [Status__c]

 

Some code from the test class:

 

 

Account a1 = new Account(Name='testkonto',
        CurrencyIsoCode='SEK',Country__c='Sweden',Industry='Art and Design');
        insert a1;
        a1.Status__c = 'Live';
        a1.Store_ID__c='9999';
        update a1;

 

And here's the trigger that I'm testing:

 

 

trigger mapOwnerInfoToAccount on Account_Owner_Info__c (after insert, after update) {

	List<String> storeids = new List<String>();
	Map<String, Account_Owner_Info__c> aoifs = new Map<String, Account_Owner_Info__c>();

	for(Integer i = 0; i < Trigger.new.size();i++)
	{
		if(Trigger.new[i].EID__c <> '')
		{
			storeids.add(Trigger.new[i].EID__c);
			aoifs.put(Trigger.new[i].EID__c, Trigger.new[i]);
		}
	}
	
	List<Account> accounts = [
	SELECT
		Id,
		EOI_Agree_with_pno__c,
		EOI_Number_of_Owners__c,
		EOI_Orgno__c,
		EOI_Owner_1_First_Name__c,
		EOI_Owner_1_Last_Name__c,
		EOI_Owner_1_Pno__c,
		EOI_Owner_2_First_Name__c,
		EOI_Owner_2_Last_Name__c,
		EOI_Owner_2_Pno__c,
		EOI_Submitter_name__c,
		EOI_Submitter_Pno__c,
		Store_ID__c,
		Status__c
	FROM
		Account
	WHERE
		Store_ID__c IN :storeids
	];
	
	List<Account> accountsToUpdate = new List<Account>();
	
	for(Account a : accounts)
	{
		Account_Owner_Info__c ao = aoifs.get(a.Store_ID__c);
		if(a.EOI_Agree_with_Pno__c == null && a.EOI_Submitter_Pno__c == null)
		{
			a.EOI_Agree_with_Pno__c = ao.Agree_with_Pno__c;				
			a.EOI_Number_of_Owners__c = ao.Number_of_Owners__c;
			a.EOI_Orgno__c = ao.Orgno__c;
			
			a.EOI_Owner_1_First_Name__c = ao.Owner_1_First_Name__c;
			a.EOI_Owner_1_Last_Name__c = ao.Owner_1_Last_Name__c;
			a.EOI_Owner_1_Pno__c = ao.Owner_1_Pno__c;
			
			a.EOI_Owner_2_First_Name__c = ao.Owner_2_First_Name__c;
			a.EOI_Owner_2_Last_Name__c = ao.Owner_2_Last_Name__c;
			a.EOI_Owner_2_Pno__c = ao.Owner_2_Pno__c;
			
			a.EOI_Submitter_name__c = ao.Submitter_name__c;
			a.EOI_Submitter_Pno__c = ao.Submitter_Pno__c;
			
			accountsToUpdate.add(a);
			
		}
	}
	
	update accountsToUpdate;
}

(I removed some additional variables so line 150 is the one where the update is fired).

 

 

 

Could you guys maybe give me a clue about what the problem is?

Best Answer chosen by Admin (Salesforce Developers) 
metaforcemetaforce

1. I am assuming that the first piece of code (for creating account) is the test data that you want to use in your trigger via test method, if not then please confirm.

 

2. From whatever code sample you have posted, my gut feel is that in your trigger, some existing org data (accounts) is also getting returned in your query which has corrupt data (i.e. StoreID is populated even though the Status is 'Prospecting'). This could be because the corrupt data already existed and the validatiuon rule was created at a later date, which is applicable only to new records, and also old records if they are getting updated.

 

3. So you may want to check out what all data is being returned in your test methods by printing them out through system.debug statements.

 

4. Also make generous use system.asset statement to ensure that you are using only test data created earlier during test method execution.

 

Post your feedback on both of the above items.

All Answers

metaforcemetaforce

1. I am assuming that the first piece of code (for creating account) is the test data that you want to use in your trigger via test method, if not then please confirm.

 

2. From whatever code sample you have posted, my gut feel is that in your trigger, some existing org data (accounts) is also getting returned in your query which has corrupt data (i.e. StoreID is populated even though the Status is 'Prospecting'). This could be because the corrupt data already existed and the validatiuon rule was created at a later date, which is applicable only to new records, and also old records if they are getting updated.

 

3. So you may want to check out what all data is being returned in your test methods by printing them out through system.debug statements.

 

4. Also make generous use system.asset statement to ensure that you are using only test data created earlier during test method execution.

 

Post your feedback on both of the above items.

This was selected as the best answer
frofrik2frofrik2

Actual, your second point was the solution. I did find two old accounts that wouldn't pass the validation rule, so I changed their values and now the test method do succeed.

 

I didn't know that the test method where running on existing data, so thanks for the help!