looping over a set of elements in Salesforce

3k views Asked by At

I am fetching a set of 10 records by using this soql in below apex code in sfdc. After fetching the records I need to only update a new value for ILR_c field.(ilrItemClone.ILR_c= ilrClone.id) and then need to insert the set of new 10 records with all other fields being same as original records except from ILR__c. But, my looping thru the list "ilrItem" is not working correctly and in the debug log I found I am getting the 1st set of record 10 times coming and hence its failing to insert the records in the system.

Pls let me know how can I get traverse thru the all 10 records and get the desired record. I am sure that I made a basic mistake in looping but couldnt figure it out. Thanks for ur help.

Here is the code piece.

    ilrItem = [ SELECT  Id, Name,
                        Account__c,
                        Comments__c,
                        ILR__c,
                        Precursor_Sample_Dropped__c,
                        PFE_Completed_Calls__c
                FROM ILR_Item__c
                WHERE  ILR__c = :presentId];

    for (ILR_Item__c ilrItems: ilrItem) {
        for(Integer i=0;i< ilrItem.size(); i++) {
            try {
                if (ilrItems!=null) {
                    ilrItemClone.Name= ilrItems.Name;
                    ilrItemClone.ILR__c= ilrClone.id;
                    ilrItemClone.Account__c= ilrItems.Account__c;
                    ilrItemClone.Comments__c= ilrItems.Comments__c;
                    ilrItemClone.PFE_Planned_Calls__c= ilrItems.PFE_Planned_Calls__c;
                    ilrItemClone.PFE_Completed_Calls__c= ilrItems.PFE_Completed_Calls__c;
                    ilrItemClist.add(ilrItemClone);
                }
            } catch(Exception e){}
        }
    }
    system.debug('********'+ilrItemClist);
    insert ilrItemClist;
}
1

There are 1 answers

1
Pavel Slepiankou On BEST ANSWER

You do not need the second inner loop here, just remove it. Also you need to create a new instance of ilrItems each time when you add a new values.

    ilrItem = [ SELECT  Id, Name,
                        Account__c,
                        Comments__c,
                        ILR__c,
                        Precursor_Sample_Dropped__c,
                        PFE_Completed_Calls__c
                FROM ILR_Item__c
                WHERE  ILR__c = :presentId];

    for (ILR_Item__c ilrItems: ilrItem) {
        try {
            if (ilrItems!=null) {
            // I do not know the type of object for ilrItemClone variable, because you define it above 
            ilrItemClone = new ilrItemClone__c(); 
            ilrItemClone.Name= ilrItems.Name;
            ilrItemClone.ILR__c= ilrClone.id;
            ilrItemClone.Account__c= ilrItems.Account__c;
            ilrItemClone.Comments__c= ilrItems.Comments__c;
            ilrItemClone.PFE_Planned_Calls__c= ilrItems.PFE_Planned_Calls__c;
            ilrItemClone.PFE_Completed_Calls__c= ilrItems.PFE_Completed_Calls__c;
            ilrItemClist.add(ilrItemClone);
            }
        } catch(Exception e){}
    }
    system.debug('********'+ilrItemClist);
    insert ilrItemClist;
}

The other trouble is catching the common exception. It's a bad practice.