Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a collection of list which is,

List<List<String>> 

Now, i am iterating it as follows,

for(List<List<String>> n: mapSAP.values()) {
                        for(List<String> l: n) {
                            System.debug('internal list:'+l);
                            System.debug('internal list size:'+l.size());                                
                            SalesOrder sobj = new SalesOrder();
                            sobj.OrderNo = l[3];
                            sobj.SalesOrg = l[4];
                            sobj.DocumentDate = Date.valueOf(l[5]); //Utility.FormatDate(l[5]); 
                            sobj.PurchaseOrderNo = l[6];
                            sobj.Amount = l[7];
                            amt += Double.valueOf(l[7]);
                            sobj.Curren = l[8];
                            if(l.size() == 11) {
                            sobj.Product = l[10];
                            } else {
                            sobj.Product = '';
                            }
                            lstSalesOrders.add(sobj);
                            cnt++;
                        }

The internal list "l" comes in varying size sometimes it comes with size 8 sometimes it comes as 9 or 10 or 11. Now i am checking the list size using if condition (like if(l.size() == 11 then access 11th element) before accessing the data using the index value like l[10] or l[9]. Is there any other best approach to deal or access the elements of list with varying size without much conditional checks as i did above?

Otherwise, without if conditions i am getting List out of bound exceptions when the list comes with size 10 and trying to access the data at 11th position. For the above usecase, the maximum list size is 11.

Update:

Salesorder is the custom wrapper class and not an Sobject.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Sometimes, I use patterns like this where I don't want to go through the trouble of defining an SObject to provide the mapping rules (which I would do if the rules were to change often or be expanded over time)

Map<String,Map<Integer,Integer> fldnameToListSizeToListPosMap = 
     new Map<String,Map<Integer,Integer> {
        'orderno' => new Map<Integer,Integer> {8 => 3, 9=> 3, 10=> 3, 11=>3},
        'product' => new Map<Integer,Integer> {8 => 8, 9=> 9, 10=> 10, 11=>10},
        ...
     };

sObj.orderNo = fldnameToListSizeToListPosMap.get('orderno').get(l.size()); 
sObj.product = fldnameToListSizeToListPosMap.get('product').get(l.size());
...

Makes code look cleaner than lots of if statements. The above presumes some regularity in expected list sizes but you should get the idea.

share|improve this answer

You could use ternary operator, e.g.:

for (...) {
    for (...) {
        sobj.A = (l.size() >= 1)  ? l[0]  : '';
        sobj.B = (l.size() >= 2)  ? l[1]  : '';
        ...
        sobj.J = (l.size() >= 10) ? l[9]  : '';
        sobj.K = (l.size() >= 11) ? l[10] : '';
    }
}

Don't think there really is a best approach in this case. It's just the matter of taste and/or habit.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.