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
BenzyBenzy 

Test Class problem

Hi, I have written a test class that keeps failing because of a FIELD_FILTER_VALIDATION_EXCEPTION. I have narrowed it down to the exact problem: Part of a lookup filter (that is looking up a Cash Account) specifies that returned records must have their Status__c = 'Active' (see full lookup filter below). When I remove this part of the filter lookup logic, the test class passes. When I add it back in, the test class fails again.

As you can see in my test class below, I have definitely set the Status__c for the Cash Account record insert to be 'Active'. So I just can't figure out what I'm missing that is causing the test class to fail based on this.

Any help much appreciated.

Lookup Filter:
User-added image

Validation Error:
User-added image

Test Class:
@isTest
 private class TestCashAccountRollUpCashIn {static testMethod void myUnitTest() {Profile pf = [Select Id from Profile where Name = 'System Administrator'];User u = new User();
 u.FirstName = 'Test';
 u.LastName = 'User';
 u.Email = 'testuser@test123456789.com';
 u.CompanyName = 'test.com';
 u.Title = 'Test User';
 u.Username = 'testuser@test123456789.com';
 u.Alias = 'testuser';
 u.CommunityNickname = 'Test User';
 u.TimeZoneSidKey = 'America/Mexico_City';
 u.LocaleSidKey = 'en_US';
 u.EmailEncodingKey = 'ISO-8859-1';
 u.ProfileId = pf.Id;
 u.LanguageLocaleKey = 'en_US';
 insert u;
system.runAs(u){
//Set RecordTypeId variable CA
 String strcaRecordTypeId = [Select Id From RecordType WHERE DeveloperName = 'Person' AND sobjectType = 'Cash_Account__c'].Id;
//Insert a cash account 
 Cash_Account__c ca = new Cash_Account__c();
 ca.RecordTypeId = strcaRecordTypeId;
 ca.Name = 'Test Person';
 ca.Status__c = 'Active';
insert ca;
system.assertEquals(ca.Rollup_Cash_In__c, null);
//Set RecordTypeId variable CT
 String strctRecordTypeId = [Select Id From RecordType WHERE DeveloperName = 'Person_to_Person' AND sobjectType = 'Cash_Transfer__c'].Id;
//Test payments on insert
 Cash_Transfer__c ct1 = new Cash_Transfer__c();
 ct1.RecordTypeId = strctRecordTypeId;
 ct1.Destination__c = ca.Id;
 ct1.Amount__c = 100;
 insert ct1;
Cash_Account__c cau1 = [select Rollup_Cash_In__c from Cash_Account__c where Id = :ca.Id];
 system.assertEquals(cau1.Rollup_Cash_In__c,ct1.Amount__c);
//Test payments on update
 Cash_Transfer__c ct1u = [select Amount__c from Cash_Transfer__c where Id = :ct1.Id];
 ct1u.Amount__c = 200;
 update ct1u;
Cash_Account__c cau2 = [select Rollup_Cash_In__c from Cash_Account__c where Id = :ca.Id];
 system.assertEquals(cau2.Rollup_Cash_In__c,ct1u.Amount__c);
//Test payments on second insert
 Cash_Transfer__c ct2 = new Cash_Transfer__c();
 ct2.RecordTypeId = strctRecordTypeId;
 ct2.Destination__c = ca.Id;
 ct2.Amount__c = 800;
 insert ct2;
AggregateResult ag1 = [select sum(Amount__c) from Cash_Transfer__c where Destination__c = :ca.Id];
Cash_Account__c cau3 = [select Rollup_Cash_In__c from Cash_Account__c where Id = :ca.Id];
 system.assertEquals(cau3.Rollup_Cash_In__c,ag1.get('expr0'));
//Test payment on delete
 delete ct2;
AggregateResult ag2 = [select sum(Amount__c) from Cash_Transfer__c where Destination__c = :ca.Id];
Cash_Account__c cau4 = [select Rollup_Cash_In__c from Cash_Account__c where Id = :ca.Id];
 system.assertEquals(cau4.Rollup_Cash_In__c,ag2.get('expr0'));
}
}
}

 
MaddyConnectMaddyConnect
It seems like Cash Transfer record ID fetched to insert Cash Transfer is not fulfilling lookup filer criteria, Can you post 1st criteria in Lookup Filter for 'Cash Transfer: Record Type'?

Thanks,
Maddy!
Vishal NegandhiVishal Negandhi
Yes, even I think the issue isn't for the 5th filter of Active record. It's because of the 1-2 or 3-4 combination. We can help you better if you show all the selected options in the filter.
BenzyBenzy
Sure sorry didn't see that it was cutoff. See attached image

User-added image
MaddyConnectMaddyConnect
Hi Banzy,

Sorry for delay in reply.

I found nothing wrong with code. I drill down with multiple possibilities by reproduing at developer account. However I will suggest to clean up existing data in object 'Cash Transfer' to adhere new lookup filter criteria for field 'Destination' (lookup for obejct 'Cash Account').

Existing will be possible cause of error message.

Thanks,
Mahadev
BenzyBenzy
Hi Maddy

Thanks for having a look a this for me. Good to know that the code seems ok.

In terms of cleaning up existing records, since it seems to be the 'Active' part causing the trouble, that would mean I have to reactivate old accounts that are now Inactive, which doesnt really seem like a longterm solution. Is that what you mean?

Thanks again.
MaddyConnectMaddyConnect
You have below options 1. You have to activate only records those are already referred in lookup field or have to replace with active accounts. 2. You don't want to update either of the existing records then you can change look-up filter from 'Required' to 'Optional'. Thanks, Maddy
BenzyBenzy
Thanks Maddy, I think that is what I will have to do.

Do you think this is worth lodging a case with Salesforce?
MaddyConnectMaddyConnect
I don't think logging case will help but you can check with them. Thanks, Maddy