0

I have the following apex code where I do a soql. In my developer sandbox my testclass gives me 100% coverage. When I deploy this to production I get the error:

System.ListException: List index out of bounds: 0

My error is on the line with: AFAS_ID__c= afas1[0].id

if(acc[x].BUMA_Achtergrondmuziek__c !='N.v.t.'){  
                List <AFAS_Artikel__c> afas1= [SELECT id, AFAS_Code__c   FROM AFAS_Artikel__c WHERE AFAS_Code__c  = : acc[x].BUMA_Achtergrondmuziek__c LIMIT 1];
            Factuurregel__c fa =new Factuurregel__c(Organisatie__c=acc[x].id, Debiteur__c = acc[x].Debiteurennummer__c ,
                                                    Aantal__c = 1 
                                                    ,  AFAS_ID__c= afas1[0].id
                                                    , Factuurdatum__c= date.today(),Referentie__c = ''+ System.Today().year());
           } 
2
  • afas1 is likely empty.
    – Girbot
    Commented Feb 15, 2018 at 12:41
  • Is your test running in "see all data" mode? If so that'd explain the difference between environments. In which case, create some sample data in the unit test instead of relying on org data.
    – Charles T
    Commented Feb 15, 2018 at 12:52

1 Answer 1

0

try to look first if your list is empty. sth like this

if(acc.size() > 1 && acc[x].BUMA_Achtergrondmuziek__c !='N.v.t.'){

      List <AFAS_Artikel__c> afas1= [SELECT id, AFAS_Code__c   FROM AFAS_Artikel__c WHERE AFAS_Code__c  = : acc[x].BUMA_Achtergrondmuziek__c LIMIT 1];
       if (afas1.size() > 1) {
             Factuurregel__c fa =new Factuurregel__c(Organisatie__c=acc[x].id, Debiteur__c = acc[x].Debiteurennummer__c ,
                                                Aantal__c = 1 
                                                ,  AFAS_ID__c= afas1[0].id
                                                , Factuurdatum__c= date.today(),Referentie__c = ''+ System.Today().year());}}

This way you are making sure, that you will not try to get an element from any empty list.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .