I have a list of string that is built dynamically (so size is not always the same). I am trying to access specific indexes in the list but only if it exists/has value.
for(Id ids : uniqueHeadersToClone.keySet()){
for(APTPS_Quote_Output_Footer1__c foot : mapHeaderIdToFooterList.get(ids)){
splitFooterArray = String.join(innerMapStringOfFooterValues.get(ids), '££').split('££');
newFooterToClone = new Footer1__c(Header__c = uniqueHeadersToClone.get(ids).Id, Value1__c = String.isNotBlank(splitFooterArray.get(1)) ? splitFooterArray.get(1).trim() : null, Value2__c = String.isNotBlank(splitFooterArray.get(2)) ? splitFooterArray.get(2).trim() : null, Value3__c = String.isNotBlank(splitFooterArray.get(3)) ? splitFooterArray.get(3).trim() : null, Value4__c = String.isNotBlank(splitFooterArray.get(4)) ? splitFooterArray.get(4).trim() : null);
newFooterToCloneList.add(newFooterToClone);
}
}
insert newFooterToCloneList;
this throws up
list index out of bounds: 4
error because 4 is the last index. Is there a way to try catch the error but still able to save object, with indexes 0 to 3?
thanks