• Michelle Bilodeau 3
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
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;        
    }
    }
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;        
    }
    }