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
Rst123Rst123 

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Account__c]: [Account__c]

I'm getting this error while inserting vloc
I have highlighted the method below :-


@IsTest(SeeAllData=False)


   private class TestCreditApprovalUtility {
   
    static testmethod void testCalculateAppRemAmount()
      {
            Set<String> caOwnerId = new Set<String>();
      User u;
       Account a;
      User thisUser = [ select Id,UserRole.Name,Profile.Name from User where Id = :UserInfo.getUserId() ];
           //UserRole r = [select id,UserRole.Name,Profile.Name from User where id = :caOwnerId]; 
         System.runAs ( thisUser ) {
  

         Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        
      u = new User(Alias = 'standt', Email='standarduser@testorg.com',userroleid = thisUser.UserRole.Id,
      EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
      LocaleSidKey='en_US'
      , ProfileId = p.Id ,TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testGE.com');

          insert u;
          }
          Set<Id> AccountId = new Set<Id>();      
          Set<Id> ParentId = new Set<Id>();
      Set<Id> OpIds = new Set<Id>();

      Set<Id> CaIds = new Set<Id>();
       List<Credit_Approval__c> CAList= new List<Credit_Approval__c>();
       List<Credit_Approval__c> CreditLineList= new List<Credit_Approval__c>();

                              //Opportunity[] opps=[Select Credit_approval__c from Opportunity where Id in :caopps.keyset()];
        //Account account = [Select Id,Name,ParentId from Account where Id in :AccountId];      
        //Create test account
       
        List<Account> acct = new List<Account>();
        Account account = new Account();
         account.recordtype=new recordtype(name='Customer Record Type');
        account.Name = 'Test';
        account.BillingStreet = '123 Some St.';
        account.BillingCity = 'Somewhere';
        account.BillingState = 'CT';
        account.BillingPostalCode = '12345';
        account.ParentId = account.Id;
        System.runAs(u) {
         
        insert account;
        }

       
        // Create a new Partner Account
        Account partner = new Account();
        partner.recordtype=new recordtype(name='Partner Record Type');
        partner.Name = 'Test Partner';
        partner.BillingStreet = '123 Some St.';
        partner.BillingCity = 'Somewhere';
        partner.BillingState = 'CT';
        partner.BillingPostalCode = '12345';
        insert partner;
      
        // Create a test opportunity to associate with Credit Approvals and Quote
        List<Opportunity> OpList= new List<Opportunity>();
        Opportunity opportunity = new Opportunity();
        opportunity.OwnerId = userinfo.getUserId();
        opportunity.AccountId = account.Id;
        opportunity.Name = 'Test';
        opportunity.StageName = 'Lead';
        opportunity.CloseDate = Date.today();
        opportunity.Amount=1000;
        
        OpList.add(opportunity);    
        insert OpList;
       
               
        for(Opportunity o : OpList){
            OpIds.add(o.Id);
        }
        //Changes made for Audit Cleanup :Increase test class coverage
        //Creating test Quote
        Quote__c quote= new Quote__c();
        quote.Product__c='FMV';
        quote.Opportunity__c=opportunity.id;
        quote.Include_For_Risk__c=true;
       
        insert quote;
       
        //End of Changes made for Audit Cleanup :Increase test class coverage
       
        //Create test takedowns to associate with opportunity
        List<Takedown__c> takedowns= new List<Takedown__c>();
        Takedown__c Tk1= new Takedown__c();
        Tk1.Name= 'Test T1';
        Tk1.Opportunity__c= opportunity.id;
        Tk1.CurrencyIsoCode= 'USD';
        Tk1.Total_Amount_Financed__c=1000.00;
       
        takedowns.add(Tk1);
       
        Takedown__c Tk2= new Takedown__c();
        Tk2.Name= 'Test T2';
        Tk2.Opportunity__c= opportunity.id;
        Tk2.CurrencyIsoCode= 'USD';
        Tk2.Total_Amount_Financed__c=700.00;
       
        takedowns.add(Tk2);
       
        insert takedowns;
       
        Utility.isOwnerUpdate=true;
        Utility.IsStageChange=true;
                 
       
                       

                   
              
      }
     
     
      // =========Second test method================
             static testmethod void testCreditApproval()
    {
    Set<Id> OpIds = new Set<Id>();
    Set<Id> CaIds = new Set<Id>();
   Account account = new Account();
   Account partner = new Account();
   Opportunity opportunity = new Opportunity();

     List<Credit_Approval__c> CAList= new List<Credit_Approval__c>();
       List<Credit_Approval__c> CreditLineList= new List<Credit_Approval__c>();

     //Create test Credit Approvals
          Credit_Approval__c CA1=new Credit_Approval__c();
        CA1.Partner__c=partner.id;
        CA1.Account__c=account.id ;
        CA1.recordtype=new recordtype(name='EF HFS Credit Approval Record Type');
        CA1.Approved_Amount_With_Tolerance__c= 10000;
        CA1.Approved_Amount__c= 10000;
        CA1.Application_Type__c= 'Line Of Credit';
        CA1.Submittal_Status__c= 'Decisioned';
        CA1.Risk_Decision__c='Approved';
        CA1.LOC_Adjustment_Amount__c= 200.00;
        //CA1.Approval_Expiration_Date__c= myDate;
        CAList.add(CA1);
       
       
        Credit_Approval__c CA2=new Credit_Approval__c();
        CA2.Partner__c=partner.id;
        CA2.Account__c=account.id ;
        CA2.recordtype=new recordtype(name='EF HFS Credit Approval Record Type');
        CA2.Approved_Amount_With_Tolerance__c= 100.00;
        CA2.Approved_Amount__c= 10000;
        CA2.LOC_Adjustment_Amount__c= 200.00;
        CA2.Application_Type__c= 'Line Of Credit';
        CA2.Submittal_Status__c= 'Referred';
        CA2.Risk_Decision__c='Approved';
        //CA2.Credit_Line_ID__c= creditLine2.id;
        //CA2.Approval_Expiration_Date__c= myDate;
        CAList.add(CA2);
       
        Credit_Approval__c CA3=new Credit_Approval__c();
        CA3.Partner__c=partner.id;
        CA3.Account__c=account.id ;
        CA3.recordtype=new recordtype(name='EF HFS Credit Approval Record Type');
        CA3.Approved_Amount_With_Tolerance__c= 100.00;
        CA3.Approved_Amount__c= 10000;
        CA3.LOC_Adjustment_Amount__c= 200.00;
        CA3.Application_Type__c= 'Deal - Multiple Takedown';
        CA3.Submittal_Status__c= 'Test';
        CA3.Risk_Decision__c='Approved';
        //CA3.Credit_Line_ID__c= creditLine2.id;
        //CA3.Approval_Expiration_Date__c= myDate;
        CA3.Opportunity__c=opportunity.id;
        CAList.add(CA3);
       
        Credit_Approval__c CA4=new Credit_Approval__c();
        CA4.Partner__c=partner.id;
        CA4.Account__c=account.id ;
        CA4.recordtype=new recordtype(name='EF HFS Credit Approval Record Type');
        CA4.Approved_Amount_With_Tolerance__c= 10000.00;
        CA4.Approved_Amount__c= 10000;
        CA4.LOC_Adjustment_Amount__c= 200.00;
        CA4.Application_Type__c= 'Deal - Multiple Takedown';
        CA4.Submittal_Status__c= 'Decisioned';
        CA4.Risk_Decision__c='Approved';
        //CA4.Credit_Line_ID__c= creditLine2.id;
        //CA4.Approval_Expiration_Date__c= myDate;
        CA4.Opportunity__c=opportunity.id;
        CAList.add(CA4);      
        
        insert CAList;
       
           for(Credit_Approval__c c : CAList){
            CaIds.add(c.Id);
        }
           

    //Create test Credit Approvals to make them as child Credit Approvals
        Credit_Approval__c creditLine1 =new Credit_Approval__c();
        creditLine1.Account__c=account.id ;
        creditLine1.recordtype=new recordtype(name='EF HFS Credit Approval Record Type');
        creditLine1.Application_Type__c= 'Deal - Multiple Takedown';
        creditLine1.Approved_Amount_With_Tolerance__c= 1000.00;
        creditLine1.Approved_Amount__c= 200.00;
        creditLine1.Submittal_Status__c= 'Decisioned';
        creditLine1.Risk_Decision__c='Approved';
        creditLine1.Credit_Line_ID__c= CA1.id;
       
        CreditLineList.add(creditLine1);
       
        Credit_Approval__c creditLine2 =new Credit_Approval__c();
        creditLine2.Account__c=account.id ;
        creditLine2.recordtype=new recordtype(name='EF HFS Credit Approval Record Type');
        creditLine2.Application_Type__c= 'Deal - Multiple Takedown';
        creditLine2.Approved_Amount_With_Tolerance__c= 100.00;
        creditLine2.Approved_Amount__c= 200.00;
        creditLine2.Submittal_Status__c= 'Referred';
        creditLine2.Risk_Decision__c='Approved';
        creditLine2.Credit_Line_ID__c= CA2.id;
        creditLine2.LOC_Adjustment_Amount__c= 200.00;
       
        CreditLineList.add(creditLine2);
       
        Credit_Approval__c creditLine3 =new Credit_Approval__c();
        creditLine3.Account__c=account.id ;
        creditLine3.recordtype=new recordtype(name='EF HFS Credit Approval Record Type');
        creditLine3.Application_Type__c= 'Deal - Multiple Takedown';
        creditLine3.Approved_Amount_With_Tolerance__c= 100.00;
        creditLine3.Approved_Amount__c= 200.00;
        creditLine3.Submittal_Status__c= 'Referred';
        creditLine3.Risk_Decision__c='Approved';
        creditLine3.Credit_Line_ID__c= null;
        creditLine3.LOC_Adjustment_Amount__c= 200.00;
       
        CreditLineList.add(creditLine3);     
               
        insert CreditLineList;

    Test.startTest();
        //CA1.Approved_Amount_With_Tolerance__c= 20000;
        //update CAList;
              
        Map<Id,Decimal> UsedAmt= CreditApprovalUtility.GetUsedAmounts(CaIds);
        List<Credit_Approval__c> ca = [Select Id,Amount_Approved_Remaining__c from Credit_Approval__c where Id in :CaIds];
        Map<Id,decimal> RemAm= CreditApprovalUtility.CalculateAppRemAmount(null);
        Map<Id,decimal> RemAmount= CreditApprovalUtility.CalculateAppRemAmount(CAList);
        //system.assertEquals(9800.00,RemAmount.get(CA1.id));
         Map<Id,decimal> RemAmountt= CreditApprovalUtility.CalculateAppRemAmount(CreditLineList);
       
        //Changes made for Audit Cleanup :Increase test class coverage
        Map<Id,double> PaymentStreamTerm= CreditApprovalUtility.GetPaymentStreamTerm(OpIds);
        //system.assertEquals(0.0,PaymentStreamTerm.get(opportunity.id));
        //End of Changes made for Audit Cleanup :Increase test class coverage
       
        Test.stopTest();

    }
    static testmethod void testCreditApproval1()
  {
  Credit_Approval__c CA=new Credit_Approval__c();

  set<id> recordtypeids=new set<id>();
  recordtype rectyp = new recordtype();
   Set<Id> OpIds = new Set<Id>();
    Set<Id> CaIds = new Set<Id>();
    Set<Id> AccountId = new Set<Id>();      
      Set<Id> ParentId = new Set<Id>();
      Set<Id> OpId = new Set<Id>();
     Set<Id> CAPartnerIds = new Set<Id>();

   Account account = new Account();
   Account partner = new Account();
   Opportunity opportunity = new Opportunity();
       List<Credit_Approval__c> CAList= new List<Credit_Approval__c>();
       List<Credit_Approval__c> CreditLineList= new List<Credit_Approval__c>();
     VVLOC__c vloc = new VVLOC__c();
     vloc.Account__c = account.Name; 
     vloc.Vendor_Loc_Status__c = 'INA'; 
      vloc.Name = vloc.Id; 
      vloc.Vendor_Location__c ='Test';
      account.ParentId = account.Id;
      Insert vloc;
    
 
  }
}
Phillip SouthernPhillip Southern
Hi, so a couple things I noticed in the method.

1.) To reference the account in the future and link to it...you need to insert it.  So just do an insert statement "insert account;"  keep in mind you might need to populate other relevant and required fields for the account....like name, etc.

2.) for your field Account__c is that a lookup or master-detail field?  If so you need to insert the id  "Account.Id".

3.) IF you are trying to reference NAme of the account, make sure you populate a value...other you're field requirement will not be satisfied because you are passing a null value.
Rst123Rst123
I'm getting the below error
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_FILTER_VALIDATION_EXCEPTION, Value does not exist or does not match filter criteria.: [Account__c]
I have a master Look field Account__c on object :VVLOC__c
Criteria : -Account Record TypeequalsPartner Record Type
Rst123Rst123
static testmethod void testCreditApproval1()
  {
  Credit_Approval__c CA=new Credit_Approval__c();

  set<id> recordtypeids=new set<id>();
  recordtype rectyp = new recordtype();
   Set<Id> OpIds = new Set<Id>();
    Set<Id> CaIds = new Set<Id>();
    Set<Id> AccountId = new Set<Id>();      
      Set<Id> ParentId = new Set<Id>();
      Set<Id> OpId = new Set<Id>();
     Set<Id> CAPartnerIds = new Set<Id>();

   Account account = new Account();
   List<Account> acct = new List<Account>();
        //Account account = new Account();
         account.recordtype=new recordtype(name='Customer Record Type');
        account.Name = 'Test';
        account.BillingStreet = '123 Some St.';
        account.BillingCity = 'Somewhere';
        account.BillingState = 'CT';
        account.BillingPostalCode = '12345';
        account.ParentId = account.Id;
                
        insert account;

      // Create a new Partner Account
        Account partner = new Account();
        partner.recordtype=new recordtype(name='Partner Record Type');
        partner.Name = 'Test Partner';
        partner.BillingStreet = '123 Some St.';
        partner.BillingCity = 'Somewhere';
        partner.BillingState = 'CT';
        partner.BillingPostalCode = '12345';
        insert partner;

   Opportunity opportunity = new Opportunity();
  
       List<Credit_Approval__c> CAList= new List<Credit_Approval__c>();
       List<Credit_Approval__c> CreditLineList= new List<Credit_Approval__c>();
     VVLOC__c vloc = new VVLOC__c();
     vloc.Account__c = Account.ID; 
     vloc.Vendor_Loc_Status__c = 'INA'; 
      vloc.Name = vloc.Id; 
      vloc.Vendor_Location__c ='Test';
      account.ParentId = account.Id;
                     
      Insert vloc;
    
  }
Phillip SouthernPhillip Southern
that error means you have a lookup filter on the field for the object and it needs to be an account with a specific recordtype.  to adhere to that you just need the Id of the recordtype and then in your test, set the account recordtype to that value.

account.recordtypeid = 'insert recordtype id here';