You need to sign in to do that
Don't have an account?
Required Field Missing, (Only When Running Test) But I Can't Figure Out Why...
I am running a test on a trigger that creates a new 'Distribution Destination' record whenever a Case is saved with a picklist set to 'Create New Distribution Destination'.
Here's the trigger, which works when I manually test it by saving a case with the picklist set to 'Create New Distribution Destination':
I also put a note in the middle starting with DEBUG LOGS SHOW, which points out another confusing point.
Here's the error I'm getting when I run the test:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CreateNewDistributionDestination: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Relationship__c]: [Relationship__c] Trigger.CreateNewDistributionDestination: line 126, column 1: []
Stack Trace:
Class.TestCreateNewDistributionDestination.testUpdateCaseToCreateNew: line 209, column 1
The line that is referenced is when the new Distribution Lists are inserted in the trigger. Again, this seems to work when I run it manually, so I am assuming that the error is in the test class' handling of the account id.
The test code is below the trigger, thanks for any assistance that could be offered!!
trigger CreateNewDistributionDestination on Case (after insert, after update) { List<Disbursement_Destination__c> DDList = new List<Disbursement_Destination__c> (); Id DDRT; List<Case> CasesToActOn = [ SELECT c.Receiving_Account_s_Number_Case__c, c.Receiving_Account_s_Name_Case__c, c.Payee_s_Name_Case__c, c.Payee_s_Street_Case__c, c.Payee_s_City_Case__c, c.Payee_s_State_Case__c, c.Payee_s_Zip_Code_Case__c, c.Payee_s_Country_Case__c, c.Receiving_Bank_s_Name_Case__c, c.Receiving_Bank_s_ABA_ACH_Case__c, c.Receiving_Bank_s_ABA_Wire_Case__c, c.Receiving_Bank_s_Street_Case__c, c.Receiving_Bank_s_City_Case__c, c.Receiving_Bank_s_State_Case__c, c.Receiving_Bank_s_Zip_Code_Case__c, c.Receiving_Bank_s_Country_Case__c, c.Account.Id, c.RecordType.name, c.RecordType.developername, c.Existing_or_New_Disbursement_Destination__c FROM Case c WHERE Existing_or_New_Disbursement_Destination__c = 'Use/Create a New Disbursement Destination' AND c.id IN :Trigger.new FOR UPDATE ];
///// DEBUG LOGS SHOW CasesToActOn.size()=1, YET IT DOES NOT RUN THE SYSTEM.DEBUG CODE WITHIN THE IF STATEMENT, ANY IDEA WHY??? When I ran it without the If statement, I got an error that the reference was out of range. Very confused on this point. /////
system.debug('Account ID: ' + CasesToActOn.size()); if(CasesToActOn.size()>0){ system.debug('Account ID: ' + CasesToActOn.get(0).account.id); } Map<String,Id> DDRTMap = new Map<String,Id>{}; List<RecordType> DDRTList = [ SELECT r.DeveloperName, r.Id, r.Name FROM RecordType r WHERE sObjectType = 'Disbursement_Destination__c']; system.debug('LIST OF ALL DD RECORD TYPES:' + DDRTList); for(RecordType RT: DDRTList){ DDRTMap.put(RT.DeveloperName,RT.Id); } system.debug('MAPPED RECORD TYPES' + DDRTMap); for (Case ac: CasesToActOn){ system.debug('Now Im in the FOR LOOP'); IF(ac.Recordtype.developerName == 'Fund_Transfer_Wire'){ DDRT = DDRTMap.get('Disbursement_Destination_Wire'); system.debug('Wire Case RT:' + ac.recordtype.developername + ' -- DD RT:' + DDRTMap.get('Disbursement_Destination_Wire')); } Else IF(ac.Recordtype.developerName == 'Fund_Transfer_ACH'){ DDRT = DDRTMap.get('Disbursement_Destination_ACH'); system.debug('ACH Case RT:' + ac.recordtype.developername + ' -- DD RT:' + DDRTMap.get('Disbursement_Destination_ACH')); } Else IF(ac.Recordtype.developerName == 'Fund_Transfer_Check'){ DDRT = DDRTMap.get('Disbursement_Destination_Check'); system.debug('Check Case RT:' + ac.recordtype.developername + ' -- DD RT:' + DDRTMap.get('Disbursement_Destination_Check')); } if(Trigger.isUpdate){ if(ac.Existing_or_New_Disbursement_Destination__c != trigger.oldMap.get(ac.Id).Existing_or_New_Disbursement_Destination__c){ system.debug('Now Im in the ISUPDATE'); DDList.add(new Disbursement_Destination__c( Name = ac.Receiving_Account_s_Number_Case__c, Receiving_Account_s_Name__c = ac.Receiving_Account_s_Name_Case__c, Payee_s_Name__c = ac.Payee_s_Name_Case__c, Payee_s_Street__c = ac.Payee_s_Street_Case__c, Payee_s_City__c = ac.Payee_s_Street_Case__c, Payee_s_State__c = ac.Payee_s_State_Case__c, Payee_s_Zip_Code__c = ac.Payee_s_Zip_Code_Case__c, Payee_s_Country__c = ac.Payee_s_Country_Case__c, Receiving_Bank_s_Name__c = ac.Receiving_Bank_s_Name_Case__c, Receiving_Bank_s_ABA_Wire__c = ac.Receiving_Bank_s_ABA_Wire_Case__c, Receiving_Bank_s_ABA_ACH__c = ac.Receiving_Bank_s_ABA_ACH_Case__c, Receiving_Bank_s_Street__c = ac.Receiving_Bank_s_Street_Case__c, Receiving_Bank_s_City__c = ac.Receiving_Bank_s_City_Case__c, Receiving_Bank_s_State__c = ac.Receiving_Bank_s_State_Case__c, Receiving_Bank_s_Zip_Code__c = ac.Receiving_Bank_s_Zip_Code_Case__c, Receiving_Bank_s_Country__c = ac.Receiving_Bank_s_Country_Case__c, Relationship__c = ac.Account.Id, RecordTypeId = DDRT )); } ac.Existing_or_New_Disbursement_Destination__c = 'Disbursement Destination Already Populated'; } if(Trigger.isInsert){ system.debug('Now Im in the ISINSERT'); DDList.add(new Disbursement_Destination__c( Name = ac.Receiving_Account_s_Number_Case__c, Receiving_Account_s_Name__c = ac.Receiving_Account_s_Name_Case__c, Payee_s_Name__c = ac.Payee_s_Name_Case__c, Payee_s_Street__c = ac.Payee_s_Street_Case__c, Payee_s_City__c = ac.Payee_s_Street_Case__c, Payee_s_State__c = ac.Payee_s_State_Case__c, Payee_s_Zip_Code__c = ac.Payee_s_Zip_Code_Case__c, Payee_s_Country__c = ac.Payee_s_Country_Case__c, Receiving_Bank_s_Name__c = ac.Receiving_Bank_s_Name_Case__c, Receiving_Bank_s_ABA_Wire__c = ac.Receiving_Bank_s_ABA_Wire_Case__c, Receiving_Bank_s_ABA_ACH__c = ac.Receiving_Bank_s_ABA_ACH_Case__c, Receiving_Bank_s_Street__c = ac.Receiving_Bank_s_Street_Case__c, Receiving_Bank_s_City__c = ac.Receiving_Bank_s_City_Case__c, Receiving_Bank_s_State__c = ac.Receiving_Bank_s_State_Case__c, Receiving_Bank_s_Zip_Code__c = ac.Receiving_Bank_s_Zip_Code_Case__c, Receiving_Bank_s_Country__c = ac.Receiving_Bank_s_Country_Case__c, Relationship__c = ac.Account.Id, RecordTypeId = DDRT )); ac.Existing_or_New_Disbursement_Destination__c = 'Disbursement Destination Already Populated'; } } if(DDList.size()>0){ system.debug('Inserting DDList'); insert DDList; } integer i = 0; system.debug(DDList.size()); if(DDList.size()>0){ for(Case ac: CasesToActOn){ ac.Disbursement_Destination__c = DDList.get(i).id; i += 1; } system.debug('Updating CasesToActOn'); update CasesToActOn; } }
Here's the test code. I think that the account id is getting lost somewhere in the handoff from Case to Distribution Destination, but because it works when I run it manually, I am completely stumped as to what's causing the error.
Test class:
@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[] accs = new account[]{ new account ( salutation = 'Mr.', firstname = 'John', lastname = 'Sandbox1', Portfolio_Manager_Lookup__c=pmId , recordtypeid = RecId ), new account( salutation = 'Mr.', firstname = 'John', lastname = 'Sandbox2', Portfolio_Manager_Lookup__c=pmId , recordtypeid = RecId )}; insert accs; //End NEWWWWWW system.debug('Accounts Have Been Created' + accs.get(0).id + ', ' + accs.get(1).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( Account = accs.get(0), 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_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( Account = accs.get(1), 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_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( Account = accs.get(0), 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_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( Account = accs.get(1), 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_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( Account = accs.get(0), 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_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( Account = accs.get(1), 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_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('Check:' + DDRTMap.get('Fund_Transfer_Check') + ', Wire:' + DDRTMap.get('Fund_Transfer_Wire') + ', APH:' + DDRTMap.get('Fund_Transfer_ACH')); system.debug('Case 1 Account Get: ' + css.get(0).account); system.debug('Cases Have Been Created, But Not Inserted'); ////Insert the cases, update the cases needed test.starttest(); insert Css; system.debug('Inserted Css'); 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'; update Css; system.debug('Updated Css'); test.stoptest(); List<Case> insertedCases = [ SELECT Type, RecordTypeId 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('',Cs.Disbursement_Destination__c); } List<Disbursement_Destination__c> TestDDs = [ SELECT name FROM Disbursement_Destination__c]; system.debug(TestDDs); //System.Assert(TestDDs.size(),2); } }
Hey Naidu,
Thanks for the response. I tried making those two changes, and then in turn made a few more just to test, but I am still getting the same error.
I also tried separating the trigger into 2 for-loops so that I can requery the CasesToActOn in case the account.id was blank b/c the Cases hadn't been inserted yet the first time, and that still did not work. Here's my updated code, followed by the portion of the debug log that might help.
Again, I am very stuck, so any help would be very appreciated!
Trigger:
And here's the relevant portion of the debug log:
Thanks again!
Try this test class. let me know if it doesnt work.
-Naidu
Thanks again! The account Ids are populating now, but I am getting this error:
System.DmlException: Update failed. First exception on row 0 with id 500c0000000vcAMAAY; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, If 'Existing or New Disbursement Destination' is set to 'Leave Disbursement Destination Blank' or 'Use/Create a New Disbursement Destination' then the 'Disbursement Destination' field must be blank.: [Disbursement_Destination__c]
Thanks again!