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
The AdmiralThe Admiral 

Test on calculation trigger errors..

Hi there,


Writing my second Test to check calculation I did with my trigger.
Quite new to writing apex tests so I took part of the test from first one and added here in but now I am lost.

20:34:26:101 EXCEPTION_THROWN [22]|System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Order__c, Investment__c]: [Order__c, Investment__c]
@isTest 
private class TestCurrentInvestmentValue {
    
    @IsTest
    static void CurrentOrEndvalueCalculated() {
        // Create Investment_rates__c
        Investment_rates__c ir = new Investment_rates__c(FundPrice__c=100, Participation_unit__c=50);
        Test.startTest();
        // Insert
        insert ir;
        Test.stopTest();
        // Select the record to get the data modified after the trigger
        ir = [SELECT CurrentorEndvalue__c, FundPrice__c, Participation_unit__c FROM Investment_rates__c WHERE Id =: ir.Id];
        // Check the values are as you espect
        System.assertEquals(ir.FundPrice__c*ir.Participation_unit__c, ir.CurrentorEndvalue__c);
    }
    Account account = new Account (Name='Test');
        insert account;
        Investment__c invest = new Investment__c (Account__c=account.Id);
        insert invest;
        Contract contract = new Contract (AccountId=account.Id,
                                         Status='Draft',
                                         StartDate=System.today() + 30,
                                         ContractTerm=30);
        insert contract;
        Order order = new Order (AccountId=account.Id,
                                 Status='Draft',
                                 ContractId=contract.Id,
                                 EffectiveDate=System.today() + 60
                                );
       	insert order;
        Investment_rates__c investrates = new Investment_rates__c (DailyFundRecord__c=dailyfund.Id,
                                                                  Investment__c=invest.id,
                                                                  Order__c=order.id,
                                                                  FundPrice__c=400,
                                                                  Status__c='Open',
                                                                  Contract__c=contract.id);
        insert investrates;


    @IsTest
    static void CurrentOrEndvalueNotCalculated() {
        Investment_rates__c ir = new Investment_rates__c();
        Test.startTest();
        insert ir;
        Test.stopTest();
        ir = [SELECT CurrentorEndvalue__c, FundPrice__c, Participation_unit__c FROM Investment_rates__c WHERE Id =: ir.Id];
        System.assertEquals(0, ir.CurrentorEndvalue__c);
    }

}

 
Best Answer chosen by The Admiral
The AdmiralThe Admiral
Hi there,

it turns out i had twice wrong account insert which i didnt see.
Went over it and solve it, :
@isTest 
private class TestCurrentInvestmentValue {
    
    @IsTest
    static void CurrentOrEndvalueCalculated() {
        Account account = new Account (Name='Test');
        insert account;
        Investment__c invest = new Investment__c (Account__c=account.Id);
        insert invest;
        Contract contract = new Contract (AccountId=account.Id,
                                         Status='Draft',
                                         StartDate=System.today() + 30,
                                         ContractTerm=30);
        insert contract;
        Order order = new Order (AccountId=account.Id,
                                 Status='Draft',
                                 ContractId=contract.Id,
                                 EffectiveDate=System.today() + 60
                                );
         insert order;
        // Create Investment_rates__c
        Investment_rates__c ir = new Investment_rates__c(FundPrice__c=100,
                                                         Participation_unit__c=50,
                                                         Investment__c=invest.id,
                                                         Order__c=order.id);
        Test.startTest();
        // Insert
        insert ir;
        Test.stopTest();
        // Select the record to get the data modified after the trigger
        ir = [SELECT CurrentorEndvalue__c, FundPrice__c, Participation_unit__c FROM Investment_rates__c WHERE Id =: ir.Id];
        // Check the values are as you espect
        System.assertEquals(ir.FundPrice__c*ir.Participation_unit__c, ir.CurrentorEndvalue__c);
    }
    
    @IsTest
    static void CurrentOrEndvalueNotCalculated() {
        Account account = new Account (Name='Test');
        insert account;
        Investment__c invest = new Investment__c (Account__c=account.Id);
        insert invest;
        Contract contract = new Contract (AccountId=account.Id,
                                         Status='Draft',
                                         StartDate=System.today() + 30,
                                         ContractTerm=30);
        insert contract;
        Order order = new Order (AccountId=account.Id,
                                 Status='Draft',
                                 ContractId=contract.Id,
                                 EffectiveDate=System.today() + 60
                                );
         insert order;
        Investment_rates__c ir = new Investment_rates__c(Investment__c=invest.id,
                                                         Order__c=order.id);
        Test.startTest();
        insert ir;
        Test.stopTest();
        ir = [SELECT CurrentorEndvalue__c, FundPrice__c, Participation_unit__c FROM Investment_rates__c WHERE Id =: ir.Id];
        System.assertEquals(0, ir.CurrentorEndvalue__c);
    }

}

 

All Answers

AbhishekAbhishek (Salesforce Developers) 
While performing insert operation (DML), you have not set the  [Order__c, Investment__c]: [Order__c, Investment__c] in one of the objects.The field is marked as required in the object definition.

Make sure, you provide appropriate value for  [Order__c, Investment__c]: [Order__c, Investment__c] while inserting records.


For further suggestions, you can check the below,
https://salesforce.stackexchange.com/questions/32769/system-dmlexception-insert-failed-first-exception-on-row-0-first-error-requi


If it helps you and close your query by marking it as the Best answer so that it can help others in the future.

Thanks.
The AdmiralThe Admiral
Hi there,

it turns out i had twice wrong account insert which i didnt see.
Went over it and solve it, :
@isTest 
private class TestCurrentInvestmentValue {
    
    @IsTest
    static void CurrentOrEndvalueCalculated() {
        Account account = new Account (Name='Test');
        insert account;
        Investment__c invest = new Investment__c (Account__c=account.Id);
        insert invest;
        Contract contract = new Contract (AccountId=account.Id,
                                         Status='Draft',
                                         StartDate=System.today() + 30,
                                         ContractTerm=30);
        insert contract;
        Order order = new Order (AccountId=account.Id,
                                 Status='Draft',
                                 ContractId=contract.Id,
                                 EffectiveDate=System.today() + 60
                                );
         insert order;
        // Create Investment_rates__c
        Investment_rates__c ir = new Investment_rates__c(FundPrice__c=100,
                                                         Participation_unit__c=50,
                                                         Investment__c=invest.id,
                                                         Order__c=order.id);
        Test.startTest();
        // Insert
        insert ir;
        Test.stopTest();
        // Select the record to get the data modified after the trigger
        ir = [SELECT CurrentorEndvalue__c, FundPrice__c, Participation_unit__c FROM Investment_rates__c WHERE Id =: ir.Id];
        // Check the values are as you espect
        System.assertEquals(ir.FundPrice__c*ir.Participation_unit__c, ir.CurrentorEndvalue__c);
    }
    
    @IsTest
    static void CurrentOrEndvalueNotCalculated() {
        Account account = new Account (Name='Test');
        insert account;
        Investment__c invest = new Investment__c (Account__c=account.Id);
        insert invest;
        Contract contract = new Contract (AccountId=account.Id,
                                         Status='Draft',
                                         StartDate=System.today() + 30,
                                         ContractTerm=30);
        insert contract;
        Order order = new Order (AccountId=account.Id,
                                 Status='Draft',
                                 ContractId=contract.Id,
                                 EffectiveDate=System.today() + 60
                                );
         insert order;
        Investment_rates__c ir = new Investment_rates__c(Investment__c=invest.id,
                                                         Order__c=order.id);
        Test.startTest();
        insert ir;
        Test.stopTest();
        ir = [SELECT CurrentorEndvalue__c, FundPrice__c, Participation_unit__c FROM Investment_rates__c WHERE Id =: ir.Id];
        System.assertEquals(0, ir.CurrentorEndvalue__c);
    }

}

 
This was selected as the best answer