My test case is inserting and updating records that should be meeting the criteria of the following validation rule, but the test says that it is failing:
Validation Rule:
AND(
OR(
ISPICKVAL( Existing_or_New_Disbursement_Destination__c , 'Leave Disbursement Destination Blank or Partially Filled'),
ISPICKVAL( Existing_or_New_Disbursement_Destination__c , 'Use/Create a New Disbursement Destination')
),
Disbursement_Destination__c <> Null
)
The system.debugs at the end output null and 'Use/Create a New Disbursement Destination', but when the
Update Css;
line runs, it throws an error for failing the above validation rule. I know the test case is very long, but most of it is just 6 cases being inserted on a bunch of lines for cleaner formatting.
Any help on this would be appreciated, thanks!
Test Case:
@isTest
private class TestCreateNewDistributionDestination{
static testMethod void testUpdateCaseToCreateNew(){
////Query RecordType Id to Create PersonAccounts for Test////
Id RecId = [
SELECT r.Id, r.Name, r.DeveloperName, r.IsPersonType
FROM RecordType r
WHERE sObjectType = 'Account' AND IsPersonType=True AND DeveloperName='Individual'
].Id;
system.debug('RecordId:' + RecId);
//NEWWWWWW
Id pmId = [
SELECT u.Id, u.Name
FROM User u
WHERE name = 'Jason Ackerman'
].Id;
Account acc1 = new account( salutation = 'Mr.', firstname = 'John', lastname = 'Sandbox1', Portfolio_Manager_Lookup__c=pmId , recordtypeid = RecId );
Account acc2 = new account( salutation = 'Mr.', firstname = 'John', lastname = 'Sandbox2', Portfolio_Manager_Lookup__c=pmId , recordtypeid = RecId );
insert acc1;
insert acc2;
//End NEWWWWWW
system.debug('Accounts Have Been Created' + acc1.id + ', ' + acc2.id);
Map<String,Id> DDRTMap = new Map<String,Id>{};
////Generate a List of Disbursement Destination Record Types////
List<RecordType> DDRTList = [
SELECT r.DeveloperName, r.Id, r.Name
FROM RecordType r
WHERE sObjectType = 'Case'];
////Put the List into a Map////
for(RecordType RT: DDRTList){
DDRTMap.put(RT.DeveloperName,RT.Id);
}
system.debug('<<<<ABOUT TO INSERT CASES>>>>');
////Create 6 Cases for the Test; 2 of each of 3 record types; 1 insert, 1 update of each////
Case[] Css = new Case[]{
new Case(
AccountId = acc1.Id,
Type = 'Wire Request',
RecordTypeId = DDRTMap.get('Fund_Transfer_Wire'),
Existing_or_New_Disbursement_Destination__c = 'Use/Create a New Disbursement Destination',
Receiving_Account_s_Number_Case__c = '135792468',
Receiving_Account_s_Name_Case__c = 'TEST Receiving Account',
//Payee_s_Name_Case__c = accs.get(0).name,
Payee_s_Name_Case__c = acc1.name,
Payee_s_Street_Case__c = '1 Test Lane',
Payee_s_City_Case__c = 'Testville',
Payee_s_State_Case__c = 'NJ',
Payee_s_Zip_Code_Case__c = '12345',
Receiving_Bank_s_Name_Case__c = 'TEST BANK',
Receiving_Bank_s_ABA_ACH_Case__c = '123456789',
Receiving_Bank_s_ABA_Wire_Case__c= '123456789',
Receiving_Bank_s_Street_Case__c = '1 Bank Street',
Receiving_Bank_s_City_Case__c = 'Bankstown',
Receiving_Bank_s_State_Case__c = 'NJ',
Receiving_Bank_s_Zip_Code_Case__c = '98765'
),
new Case(
AccountId = acc2.Id,
Type = 'Wire Request',
RecordTypeId = DDRTMap.get('Fund_Transfer_Wire'),
Existing_or_New_Disbursement_Destination__c = 'Leave Disbursement Destination Blank',
Receiving_Account_s_Number_Case__c = '135792468',
Receiving_Account_s_Name_Case__c = 'TEST Receiving Account',
//Payee_s_Name_Case__c = accs.get(1).name,
Payee_s_Name_Case__c = acc2.name,
Payee_s_Street_Case__c = '1 Test Lane',
Payee_s_City_Case__c = 'Testville',
Payee_s_State_Case__c = 'NJ',
Payee_s_Zip_Code_Case__c = '12345',
Receiving_Bank_s_Name_Case__c = 'TEST BANK',
Receiving_Bank_s_ABA_ACH_Case__c = '123456789',
Receiving_Bank_s_ABA_Wire_Case__c= '123456789',
Receiving_Bank_s_Street_Case__c = '1 Bank Street',
Receiving_Bank_s_City_Case__c = 'Bankstown',
Receiving_Bank_s_State_Case__c = 'NJ',
Receiving_Bank_s_Zip_Code_Case__c = '98765'
),
new Case(
AccountId = acc1.Id,
Type = 'Wire Request',
RecordTypeId = DDRTMap.get('Fund_Transfer_ACH'),
Existing_or_New_Disbursement_Destination__c = 'Use/Create a New Disbursement Destination',
Receiving_Account_s_Number_Case__c = '135792468',
Receiving_Account_s_Name_Case__c = 'TEST Receiving Account',
//Payee_s_Name_Case__c = accs.get(0).name,
Payee_s_Name_Case__c = acc1.name,
Payee_s_Street_Case__c = '1 Test Lane',
Payee_s_City_Case__c = 'Testville',
Payee_s_State_Case__c = 'NJ',
Payee_s_Zip_Code_Case__c = '12345',
Receiving_Bank_s_Name_Case__c = 'TEST BANK',
Receiving_Bank_s_ABA_ACH_Case__c = '123456789',
Receiving_Bank_s_ABA_Wire_Case__c= '123456789',
Receiving_Bank_s_Street_Case__c = '1 Bank Street',
Receiving_Bank_s_City_Case__c = 'Bankstown',
Receiving_Bank_s_State_Case__c = 'NJ',
Receiving_Bank_s_Zip_Code_Case__c = '98765'
),
new Case(
AccountId = acc2.Id,
Type = 'Wire Request',
RecordTypeId = DDRTMap.get('Fund_Transfer_ACH'),
Existing_or_New_Disbursement_Destination__c = 'Leave Disbursement Destination Blank',
Receiving_Account_s_Number_Case__c = '135792468',
Receiving_Account_s_Name_Case__c = 'TEST Receiving Account',
//Payee_s_Name_Case__c = accs.get(1).name,
Payee_s_Name_Case__c = acc2.name,
Payee_s_Street_Case__c = '1 Test Lane',
Payee_s_City_Case__c = 'Testville',
Payee_s_State_Case__c = 'NJ',
Payee_s_Zip_Code_Case__c = '12345',
Receiving_Bank_s_Name_Case__c = 'TEST BANK',
Receiving_Bank_s_ABA_ACH_Case__c = '123456789',
Receiving_Bank_s_ABA_Wire_Case__c= '123456789',
Receiving_Bank_s_Street_Case__c = '1 Bank Street',
Receiving_Bank_s_City_Case__c = 'Bankstown',
Receiving_Bank_s_State_Case__c = 'NJ',
Receiving_Bank_s_Zip_Code_Case__c = '98765'
),
new Case(
AccountId = acc1.Id,
Type = 'Wire Request',
RecordTypeId = DDRTMap.get('Fund_Transfer_Check'),
Existing_or_New_Disbursement_Destination__c = 'Use/Create a New Disbursement Destination',
Receiving_Account_s_Number_Case__c = '135792468',
Receiving_Account_s_Name_Case__c = 'TEST Receiving Account',
//Payee_s_Name_Case__c = accs.get(0).name,
Payee_s_Name_Case__c = acc1.name,
Payee_s_Street_Case__c = '1 Test Lane',
Payee_s_City_Case__c = 'Testville',
Payee_s_State_Case__c = 'NJ',
Payee_s_Zip_Code_Case__c = '12345',
Receiving_Bank_s_Name_Case__c = 'TEST BANK',
Receiving_Bank_s_ABA_ACH_Case__c = '123456789',
Receiving_Bank_s_ABA_Wire_Case__c= '123456789',
Receiving_Bank_s_Street_Case__c = '1 Bank Street',
Receiving_Bank_s_City_Case__c = 'Bankstown',
Receiving_Bank_s_State_Case__c = 'NJ',
Receiving_Bank_s_Zip_Code_Case__c = '98765'
),
new Case(
AccountId = acc2.Id,
Type = 'Wire Request',
RecordTypeId = DDRTMap.get('Fund_Transfer_Check'),
Existing_or_New_Disbursement_Destination__c = 'Leave Disbursement Destination Blank',
Receiving_Account_s_Number_Case__c = '135792468',
Receiving_Account_s_Name_Case__c = 'TEST Receiving Account',
//Payee_s_Name_Case__c = accs.get(1).name,
Payee_s_Name_Case__c = acc2.name,
Payee_s_Street_Case__c = '1 Test Lane',
Payee_s_City_Case__c = 'Testville',
Payee_s_State_Case__c = 'NJ',
Payee_s_Zip_Code_Case__c = '12345',
Receiving_Bank_s_Name_Case__c = 'TEST BANK',
Receiving_Bank_s_ABA_ACH_Case__c = '123456789',
Receiving_Bank_s_ABA_Wire_Case__c= '123456789',
Receiving_Bank_s_Street_Case__c = '1 Bank Street',
Receiving_Bank_s_City_Case__c = 'Bankstown',
Receiving_Bank_s_State_Case__c = 'NJ',
Receiving_Bank_s_Zip_Code_Case__c = '98765'
)
};
//system.debug('MINE: Check:' + DDRTMap.get('Fund_Transfer_Check') + ', Wire:' + DDRTMap.get('Fund_Transfer_Wire') + ', APH:' + DDRTMap.get('Fund_Transfer_ACH'));
//system.debug('MINE: Case 1 Account Get: ' + css.get(0).account);
//system.debug('MINE: Cases Have Been Created, But Not Inserted');
////Insert the cases, update the cases needed
test.starttest();
insert Css;
//system.debug('MINE: Inserted Css');
system.debug('MINE: ' + Css[1].Disbursement_Destination__c);
system.debug('MINE: ' + Css[1].Existing_or_New_Disbursement_Destination__c);
system.debug('MINE: ' + Css[2].Disbursement_Destination__c);
system.debug('MINE: ' + Css[2].Existing_or_New_Disbursement_Destination__c);
system.debug('MINE: ' + Css[3].Disbursement_Destination__c);
system.debug('MINE: ' + Css[3].Existing_or_New_Disbursement_Destination__c);
system.debug('MINE: ' + Css[4].Disbursement_Destination__c);
system.debug('MINE: ' + Css[4].Existing_or_New_Disbursement_Destination__c);
system.debug('MINE: ' + Css[5].Disbursement_Destination__c);
system.debug('MINE: ' + Css[5].Existing_or_New_Disbursement_Destination__c);
system.debug('MINE: ' + Css[0].Disbursement_Destination__c);
system.debug('MINE: ' + Css[0].Existing_or_New_Disbursement_Destination__c);
Css[1].Disbursement_Destination__c = Null;
Css[3].Disbursement_Destination__c = Null;
Css[5].Disbursement_Destination__c = Null;
Css[1].Existing_or_New_Disbursement_Destination__c = 'Use/Create a New Disbursement Destination';
Css[3].Existing_or_New_Disbursement_Destination__c = 'Use/Create a New Disbursement Destination';
Css[5].Existing_or_New_Disbursement_Destination__c = 'Use/Create a New Disbursement Destination';
system.debug('MINE: Case 1 DD: ' + Css[1].Disbursement_Destination__c + ' and Existing or New? ' + Css[1].Existing_or_New_Disbursement_Destination__c);
update Css;
system.debug('Updated Css');
test.stoptest();
List<Case> insertedCases = [
SELECT Type, RecordTypeId, Existing_or_New_Disbursement_Destination__c, Disbursement_Destination__c
FROM Case
WHERE Id IN :Css];
system.debug(insertedCases.size() + ' Cases Have Been Created');
system.debug(insertedCases);
System.AssertEquals(insertedCases.size(),6);
for(Case Cs : insertedCases ){
System.AssertEquals('Disbursement Destination Already Populated',Cs.Existing_or_New_Disbursement_Destination__c);
System.AssertNotEquals(Null,Cs.Disbursement_Destination__c);
}
List<Disbursement_Destination__c> TestDDs = [
SELECT name
FROM Disbursement_Destination__c];
system.debug(TestDDs);
//System.Assert(TestDDs.size(),2);
}
}
Attribution to: jackerman09
Possible Suggestion/Solution #1
- Is it possible you have another validation rule in there that has a different formula on it?
- Is there a workflow field update or trigger happening on the Case?
- As mentioned above by @techtrekker, try
NOT(ISBLANK(Disbursement_Destination__c))
, which does sometimes work differently from null
Attribution to: Jeremy Nottingham
Possible Suggestion/Solution #2
I ran a simplified version of your test, using a Lookup field for Disbursement Destination (note: I copy/pasted from what you posted):
@isTest
private class TestCreateNewDistributionDestination{
static testMethod void testUpdateCaseToCreateNew(){
Case[] Css = new Case[]{
new Case(
AccountId = '001d000000J6WTn',
Origin = 'Phone',
Existing_or_New_Disbursement_Destination__c = 'Use/Create a New Disbursement Destination'
)
};
////Insert the cases, update the cases needed
test.starttest();
insert Css;
system.debug('MINE: ' + Css[0].Disbursement_Destination__c);
system.debug('MINE: ' + Css[0].Existing_or_New_Disbursement_Destination__c);
Css[0].Disbursement_Destination__c = Null;
Css[0].Existing_or_New_Disbursement_Destination__c = 'Use/Create a New Disbursement Destination';
system.debug('MINE: Case 0 DD: ' + Css[0].Disbursement_Destination__c + ' and Existing or New? ' + Css[0].Existing_or_New_Disbursement_Destination__c);
update Css;
system.debug('Updated Css');
test.stoptest();
}
}
This test passed, with the following from the log:
Insert:
17:13:28.113 (113542000)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Case:new
17:13:28.113 (113564000)|VALIDATION_RULE|03dd0000000b6ks|Disbursement_Test
17:13:28.113 (113856000)|VALIDATION_FORMULA|AND(
OR(
ISPICKVAL( Existing_or_New_Disbursement_Destination__c , 'Leave Disbursement Destination Blank or Partially Filled'),
ISPICKVAL( Existing_or_New_Disbursement_Destination__c , 'Use/Create a New Disbursement Destination')
),
Disbursement_Destination__c <> Null
)|Existing_or_New_Disbursement_Destination__c=Use/Create a New Disbursement Destination , Disbursement_Destination__c=null
17:13:28.113 (113879000)|VALIDATION_PASS
and the validation passed for the update as well. Your debug statements verified the values were as expected:
(784826000)|USER_DEBUG|[20]|DEBUG|MINE: null
(784934000)|USER_DEBUG|[21]|DEBUG|MINE: Use/Create a New Disbursement Destination
(785102000)|USER_DEBUG|[27]|DEBUG|MINE: Case 0 DD: null and Existing or New? Use/Create a New Disbursement Destination
The test was done in a Developer Org where the Case object has no other customizations, so I think it validates your assumption that your code should work.
Some ideas:
- Disable your validation rule and re-run the test. If it passes you know the validation rule is the culprit
- If it is your validation rule, try simplifying it and slowly rebuild it; maybe there's a typo you haven't noticed. I would start with just 'false', then add in the PICKVAL checks, and then the NULL check
Update: Changed Disbursement destination field to Lookup based on comments. The test still passed, so based on your additional comment of the test passing when the validation rule is disabled other possibilities are eliminated.
Attribution to: Mike Chale
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/2158