1

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);
}
}
6
  • 1
    Try NOT(ISBLANK(Disbursement_Destination__c)) Oct 10, 2012 at 18:43
  • I made the change, but I am still getting this error: System.DmlException: Update failed. First exception on row 0 with id 500L0000000rbi0IAA; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, If 'Existing or New Disbursement Destination' is set to 'Leave Disbursement Destination Blank or Partially Filled' or 'Use/Create a New Disbursement Destination' then the 'Disbursement Destination' field must be blank.: [Disbursement_Destination__c] Oct 10, 2012 at 19:05
  • Ya thats correct .Since validation rule is running you will have the Field custom validation exception thrown at time of data insert Oct 10, 2012 at 19:27
  • @mohith kumar Right, but the debug log is showing this "DEBUG| null DEBUG|Use/Create a New Disbursement Destination" for the two values, so why would that cause the validation rule to fail? This is from system debugs that I added right before the update Css; line? Oct 10, 2012 at 19:35
  • What field type is Disbursement_Destination__c?
    – Mike Chale
    Oct 10, 2012 at 23:49

2 Answers 2

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:

  1. Disable your validation rule and re-run the test. If it passes you know the validation rule is the culprit
  2. 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.

4
  • I disabled the validation rule and re-ran, and it worked, so I'm convinced that the validation rule is the issue. Ok thanks, the weird part is that it was already deployed succcessfully, and the test failed when I tried to deploy another package. I can't find anything that may have changed to cause it to fail. Oct 11, 2012 at 15:40
  • Can you recreate this in a fresh org? Setup the validation rule and APEX. Once verified working install the new package and see if it breaks. Have any other changes been made?
    – Mike Chale
    Oct 11, 2012 at 16:20
  • Unfortunately, I only have one production org and one sandbox org, neither of which I can sacrifice to test this. I think I'm going to go with the advice to reconstruct the validation rule from scratch, step by step, and see if I can find the error. Thanks again for the help! Oct 11, 2012 at 16:48
  • You can always get a Developer Edition org (not sure if the new package would work in it.)
    – Mike Chale
    Oct 11, 2012 at 17:15
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
1
  • I'm pretty sure it's this validation rule b/c when I deactivated the test ran without any failures. I don't think there are any other workflows running on cases. I already made the change to NOT(ISBLANK( and it had the same issue. Thanks for the suggestions though. Oct 11, 2012 at 1:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .