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
Michelle Bilodeau 3Michelle Bilodeau 3 

Writing a test class with required field and filter criteria

I have written a simple trigger, which works when tested manually. I am running into a problem when writing my test class and receive the error message: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, 
Required fields are missing: [Parent_Account__c]: [Parent_Account__c]  

The field Parent_Account__c is a required field with filter criteria. The criteria is that the Account Record Type EQUALS Parent Account. This is a master-detail where Account is master and Account Contract is detail. As written below no problems are highlighted and when I run the Test Class it receives 100% overall code coverage. I am having trouble figuring out how to write in the required field (Parent_Account__c) so that the insert can happen. I have tried a few approaches but continue to run into errors. Any insight would be much appreciated! 

Below is the trigger followed by my initial test class:

trigger ActiveContractCheckbox on Account_Contract__c (before update, before insert) {
    for (Account_Contract__c ac : Trigger.new) {
        If (ac.Contract_Start_Date__c <= DATE.TODAY() && ac.Contract_End_Date__c >= DATE.TODAY()){
                ac.Active_Contract__c = True;
        } Else If (ac.Contract_Start_Date__c > DATE.TODAY() || ac.Contract_End_Date__c < DATE.TODAY()){
                ac.Active_Contract__c = False;
            }   
         }
      }
  
@isTest
public class ActiveContractCheckboxTest
{
static testMethod void updateAccountContract()
    {
        Account_Contract__c myAcctContract = new Account_Contract__c();
        myAcctContract.Contract_Start_Date__c = Date.today();
        myAcctContract.Contract_End_Date__c   = Date.today();
            insert myAcctContract; 
        myAcctContract.Assigned_To__c = 'Jane Doe';
     update myAcctContract;
      }
    static testMethod void updateAccountContract2()
    {
        Account_Contract__c myAcctContract = new Account_Contract__c(); 
        Date myDate = Date.Today();
        Date newDate = myDate.addDays(3);
        Date newDate2 = myDate.addDays(-3);
        myAcctContract.Contract_Start_Date__c = newDate;
        myAcctContract.Contract_End_Date__c = newDate2;
         insert myAcctContract;
        myAcctContract.Assigned_To__c = 'Jason Doe';
         update myAcctContract;        
    }
    }
Best Answer chosen by Michelle Bilodeau 3
v varaprasadv varaprasad
Hi Michelle,

Please check once below code : 
 
@isTest
public class ActiveContractCheckboxTest
{
static testMethod void updateAccountContract()
    {
        account acc = new account();
		acc.name = 'TestAccount';
		insert acc;
		
		Account_Contract__c myAcctContract = new Account_Contract__c();
		myAcctContract.Parent_Account__c = acc.id;
        myAcctContract.Contract_Start_Date__c = Date.today();
        myAcctContract.Contract_End_Date__c   = Date.today();
            insert myAcctContract; 
        myAcctContract.Assigned_To__c = 'Jane Doe';
     update myAcctContract;
      }
    static testMethod void updateAccountContract2()
    {
	    account acc = new account();
		acc.name = 'TestAccount';
		insert acc;
		
        Account_Contract__c myAcctContract = new Account_Contract__c(); 
        Date myDate = Date.Today();
        Date newDate = myDate.addDays(3);
        Date newDate2 = myDate.addDays(-3);
		myAcctContract.Parent_Account__c = acc.id;
        myAcctContract.Contract_Start_Date__c = newDate;
        myAcctContract.Contract_End_Date__c = newDate2;
         insert myAcctContract;
        myAcctContract.Assigned_To__c = 'Jason Doe';
         update myAcctContract;        
    }
    }

Hope it helps you.
If it helps you, Please mark it as the best answer.
Please let me know in case of any other assistance.


Thanks
Varaprasad

All Answers

v varaprasadv varaprasad
Hi Michelle,

Please check once below code : 
 
@isTest
public class ActiveContractCheckboxTest
{
static testMethod void updateAccountContract()
    {
        account acc = new account();
		acc.name = 'TestAccount';
		insert acc;
		
		Account_Contract__c myAcctContract = new Account_Contract__c();
		myAcctContract.Parent_Account__c = acc.id;
        myAcctContract.Contract_Start_Date__c = Date.today();
        myAcctContract.Contract_End_Date__c   = Date.today();
            insert myAcctContract; 
        myAcctContract.Assigned_To__c = 'Jane Doe';
     update myAcctContract;
      }
    static testMethod void updateAccountContract2()
    {
	    account acc = new account();
		acc.name = 'TestAccount';
		insert acc;
		
        Account_Contract__c myAcctContract = new Account_Contract__c(); 
        Date myDate = Date.Today();
        Date newDate = myDate.addDays(3);
        Date newDate2 = myDate.addDays(-3);
		myAcctContract.Parent_Account__c = acc.id;
        myAcctContract.Contract_Start_Date__c = newDate;
        myAcctContract.Contract_End_Date__c = newDate2;
         insert myAcctContract;
        myAcctContract.Assigned_To__c = 'Jason Doe';
         update myAcctContract;        
    }
    }

Hope it helps you.
If it helps you, Please mark it as the best answer.
Please let me know in case of any other assistance.


Thanks
Varaprasad
This was selected as the best answer
Michelle Bilodeau 3Michelle Bilodeau 3
Thank you for the reply Varaprasad,

I am still getting an error when I run the Test, on line 14 and 31. When the record tries to insert it fails because the field requires that Account Record Type equals Parent Account. When manually creating a new Account Contract, Parent_Account__c muct be filled in, this is a lookup field that displays all Accounts that have an Account Record Type equal to Parent Account. So if the Parent Account does not exist in the Org the record will not save. I am thinking I need to populate the Account Record Type field on account with 'Parent Account' before the first insert? Or reference existing Accounts with Record Type = Parent Account? 

Thank you again for your help and input. 
Michelle
v varaprasadv varaprasad
Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Parent Accoun').getRecordTypeId();

Add below line two places after acc.name and check once.
Acc.recordtypeid = devRecordTypeId;

 
v varaprasadv varaprasad
.Get('parent account') add record type name correctly.
 
Michelle Bilodeau 3Michelle Bilodeau 3
That did it! Thank you very much!