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
MattwestMattwest 

Very Simple Test Class (need help)

Hello all,

I am learning apex, and have written several triggers. I thought I had figured out how to write a good test class, but unfortunately I did not. Can anyone tell me what I'm doing wrong?

 

My Trigger:

1
2
3
4
5
6
7
8 trigger scUpdate on Service_Call__c(before insert, before update){
    gii__SystemPolicy__c gii = [Select Mileage_Rate__c, Travel_Rate_Base__c, Travel_Rate__c from gii__SystemPolicy__c limit 1];
    for(Service_Call__c  sc : trigger.new){
        sc.Mileage_Rate__c = gii.Mileage_Rate__c;
        sc.Travel_Base__c = gii.Travel_Rate_Base__c;
        sc.Travel_Rate__c = gii.Travel_Rate__c;
    }
}

 

My Test Class: (not working)

1
2
3
4
5
6
7 @isTest 
private class TestscUpdate {
    static testMethod void insertNewCall() {
      Service_Call__c call = new Service_Call__c();
      insert call;
   }
}

 

The error: 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, scUpdate: execution of BeforeInsert

caused by: System.QueryException: List has no rows for assignment to SObject

Trigger.scUpdate: line 2, column 1: []
SFDC-NOOBSFDC-NOOB

You need to insert the fields in the record you are inserting in the test class.

 

You can insert fields like this:

service_call__c call = new service_call__c(mileage_rate__c = 4.3, name = 'Test');

insert call;

 

Make sure you assign a value to all required fields.

MattwestMattwest

Still getting the same error. I added all required fields, but still get the same error.

MattwestMattwest

Ok, found my issue, just added (SeeAllData=true) to my class