I am brand new to coding and this is my first test class. I have spent hours trying to get it to pass.

Apex Trigger That I am Testing

trigger RenameTransaction on Financial_Transaction__c (before insert) {

    for(Financial_Transaction__c a: Trigger.New) {
        Financial_Month__c fm = [SELECT Name FROM Financial_Month__c WHERE ID = :a.Financial_Month__c];
        Account vn = [SELECT Name FROM Account WHERE ID = :a.Vendor__c];
        a.Name = fm.Name + ' * ' + vn.Name + ' * ($' + a.Amount__c + ')';

Test Class That I Wrote

private class RenameTransactionTestClass {
    Static testMethod void validateInsertTransaction() {
        financial_transaction__c ft = new Financial_transaction__c(
            Currency_type__c = 'Debit',
            Financial_month__c = 'a016g0000021jNuAAI',
            Vendor__c = '0016g000003s4pCAAQ',
            Billing_period__c = '1st Half');

        insert ft;

        ft = [SELECT Id, Name FROM financial_transaction__c WHERE Id =:ft.Id];

        System.assertNotEquals('Test', ft.Name);


Error That I am Receiving

Class RenameTransactionTestClass Method Name validateInsertTransaction Pass/Fail Fail

Error Message System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, RenameTransaction: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject Trigger.RenameTransaction: line 4, column 1: [] Stack Trace Class.RenameTransactionTestClass.validateInsertTransaction: line 14, column 1

Any help you can provide would be greatly appreciated. I am trying to teach myself how to code.

  • Do you have a Financial_Month__c record with Id a016g0000021jNuAAI and an Account record with Id 0016g000003s4pCAAQ available in your Org? Additionally if you are trying to use existing records, you will need to ensure you are using seealldata=true. A better approach in these scenarios is to setup the data that you require. You can refer more on seealldata here – Jayant Das Mar 3 at 22:54
  • Welcome to SFSE! This is a common challenge with unit tests. I've linked a similar issue above along with our collection of standard resources on how to write great unit tests. The long and short of it is that your unit test needs to generate all of its own data within the test method - it can't see those records. More in the links above. – David Reed Mar 3 at 23:38
  • @DavidReed thank you very much, this is extremely helpful! – David Nava Mar 4 at 9:01
  • @JayantDas Yes, I am using existing records. I will set up my data instead. Many thanks!! – David Nava Mar 4 at 9:02

Browse other questions tagged or ask your own question.