TransWikia.com

Error: List index out of bounds: 1

Salesforce Asked by Samara Oliveira on November 11, 2021

I concatenate in the "Comentários" variable the values ​​of the list "lstComments" but the error in my visual force "List index out of bounds: 1"

    List<Pesquisa__c> lstPesq = [SELECT Id, InternoSolucoesInadequadasNegocio__c, InternoQueremoSuaExperienciaSejaMelhor__c, 
                                            InternoTrabParaManterRelacaoPositiva__c, InternoQueremostrabEstrRelacao__c 
                                    FROM Pesquisa__c 
                                    WHERE conta__c =: acctId AND (Tipo__c = 'NPS Semanal' OR Tipo__c = 'NPS Bimestral' )  
                                    ORDER BY CreatedDate DESC];
    List<String> lstComments = new List<String>();
    
    for(Pesquisa__c p : lstPesq){
        if(p.InternoQueremostrabEstrRelacao__c <> null){
            lstComments.add(p.InternoQueremostrabEstrRelacao__c);
        }
        if(p.InternoTrabParaManterRelacaoPositiva__c <> null){
            lstComments.add(p.InternoTrabParaManterRelacaoPositiva__c);
        }
        if(p.InternoQueremoSuaExperienciaSejaMelhor__c <> null){
            lstComments.add(p.InternoQueremoSuaExperienciaSejaMelhor__c);
        }
        if(p.InternoSolucoesInadequadasNegocio__c <> null){
            lstComments.add(p.InternoSolucoesInadequadasNegocio__c);
        }
    }
    
    for(Integer i = 0; i < 5; i++){
       System.debug(i);
        if(lstComments.size() > 0)
        comentarios += '"' + lstComments[i] + '"nn';
    }

    System.debug(lstComments);
}```

One Answer

Instead of using 5, use the size of the list:

for(Integer i = 0, s = lstComments.size(); i < s; i++){

Or, alternatively, just use String.join:

comentarios = '"'+String.join(lstComments,'"nn')+'"nn';

If you want to limit yourself to five items, you can also remove the excess items:

while(lstComments.size() > 5) {
  lstComments.remove(lstComments.size()-1);
}

Or some variant.

Alternatively, stop when you get to five comments:

for(Pesquisa__c p : lstPesq){
    if(p.InternoQueremostrabEstrRelacao__c <> null){
        lstComments.add(p.InternoQueremostrabEstrRelacao__c);
        if(lstComments.size() == 5) {
            break;
        }
    }
    if(p.InternoTrabParaManterRelacaoPositiva__c <> null){
        lstComments.add(p.InternoTrabParaManterRelacaoPositiva__c);
        if(lstComments.size() == 5) {
            break;
        }
    }
    if(p.InternoQueremoSuaExperienciaSejaMelhor__c <> null){
        lstComments.add(p.InternoQueremoSuaExperienciaSejaMelhor__c);
        if(lstComments.size() == 5) {
            break;
        }
    }
    if(p.InternoSolucoesInadequadasNegocio__c <> null){
        lstComments.add(p.InternoSolucoesInadequadasNegocio__c);
        if(lstComments.size() == 5) {
            break;
        }
    }
}

Answered by sfdcfox on November 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP