+ Start a Discussion
RadDude89RadDude89 

Trigger test failure

Hi,

I'm writing a small apex class test for a trigger we have but when I run the test it fails with the error message "Missing id at index".

This is the test class below.

@istest
public class DeleteJunk_trigger_test {

    public static testmethod void DeleteJunk_trigger()
    {
       test.startTest();
         Registrations__c reg = new Registrations__c(GPRN__c = '',  Source__c = '' , Source_System__c = '' , Region__c = '' ,     
        MPRN__c = '');
       delete reg;
       }
       }

Can anyone see what I am missing?

Thanks.

Tarun Khatri (TK)Tarun Khatri (TK)
Hello,

You first need to insert a record before delete command. Here is the update code:
 
@istest
public class DeleteJunk_trigger_test {

    public static testmethod void DeleteJunk_trigger()
    {
         Test.startTest();
         Registrations__c reg = new Registrations__c(GPRN__c = '',  Source__c = '' , Source_System__c = '' , Region__c = '' ,     
        MPRN__c = '');
         insert reg;
         delete reg;
         Test.stopTest();
       }
}

-Thanks,
TK

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Amit Chaudhary 8Amit Chaudhary 8
I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm
3) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

NOTE:- you need to insert data first then you can delete in your test class like below
public class DeleteJunk_trigger_test {

    public static testmethod void DeleteJunk_trigger()
    {
       test.startTest();
         Registrations__c reg = new Registrations__c(GPRN__c = '',  Source__c = '' , Source_System__c = '' , Region__c = '' ,     
        MPRN__c = '');
        // Please add all required field 
        insert reg;
       delete reg;
       }
       }

Let us know if this will help you