function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
EUSEUS 

List index out of bounds....

Hi,

 

I have created a list (RDML) based on a custom sObject (Rdm_Line__c) to populate  with fields from another custom sObject  (Pocket__c).  When I finish iterating over the Pocket__c object from which I also create a Map, I add the records to the RDML list.

No matter what I do (add, set) it would always failed with fatal error  List index out of bounds.....

I don't see the error, ... Can someone help me understand how should I add elements to the list avoiding such error? ...

Here follows the sample code:

 

List<Rdm_Line__c>  RDML     = new List<Rdm_Line__c>();                // Rdm_Line List.

Rdm_Line__c    lrdm                = newRdm_Line__c();                          // New Line Rdm_Line record.

Map<Id, Rdm_Line__c> mIdRL = new Map<Id, Rdm_Line__c>();         // Map that holds...

 

I checked on the Developer Console that mIdRL Map is well constructed and all records are correctly stored there.

 

so at the end of the iteration over Pocket__c I do the following:

 

if (!mIdRL.isEmpty()) {

          RDML.clear();                // Clear Rdm List.                                  

           i = 0;                             //  RDML array index is initialized.

             

for (ID pIds : mIdRL.KeySet()) {                                 // KeySet from Pocket Id Map.

              

             if (RDML.isEmpty()) {                                     // RDML List is empty so I add a new element i.

                   RDML.add(i++, mIdRL.get(pIds));                 // Populate Rdm_Line List from Pocket Map.

               }

              

             else {RDML.set(i++, mIdRL.get(pIds));}       // RDML List is NOT empty so I set the element i.

 }

             

insert RDML;                                                             // Rdm_Line records are inserted.

 

The Fatal error is on the red line above where the message reads:

System.ListException: List index out of bounds: 0

 

I would appreciate if someone could explain to me what I am doing wrong, and how List indexes should be managed....

 

Thanks a lot!

sfdcfoxsfdcfox

I believe you're trying too hard. I'd just do this:

 

insert mldRL.values();

Which is the same as what you're doing now without the extra variables, loops, etc.

 

That being said, I believe that your existing code would need to start at -1 in order to execute correctly. I don't use the two parameter version of add very often because it has a very narrow use range, such as inserting a line at an arbitrary point in the list, usually by user request. Since your goal is always to add to the end of the list, the single parameter add function would suffice, but there's really no need to be this elaborate. I'm performing the same logic as your code in exactly one line. If you also needed RDML populated, you could just RDML.addAll(mldRL.values()), and if you somehow needed i, you could just set i to mldRL.size()... But it's probably not necessary.

EUSEUS

Hi sfdcfox,

 

Thanks for your answer. Unfortunately that didn't work either. I tried with -1 and same results.... I Also tried using the Map values and didn't work.  So I finally tried using    i = RDML.size();  and also gives me the List index out of bounds!...

 

I really don't understand what is going on with that list...

 

 

sfdcfoxsfdcfox
I think we need to back up a step. You're receiving some sort of error, so you've tried all sorts of code to try and fix it, but the problem is fundamentally elsewhere. What did your code look like before you started?
EUSEUS

Hi sfdcfox,

 

Thanks for you help! Sorry I didn't answer before, I have to work on this on my spare time.

 

The code looked like this when I got the List index out of bounds for the first time: (the error pointed to the red line below)

 

 .
 List<Rdm_Line__c>  RDML  = new List<Rdm_Line__c>();     // Rdm_Line List.
 Rdm_Line__c    lrdm             = new Rdm_Line__c();               // New Line Rdm_Line record.
 integer i                                  = 0;                                             // Rdm_Line List index.     

 

 List<Pocket__c> pKs  = [SELECT p.Id, p.Pocket_Balance__c, p.certRed_Ref__c, p.certRedValue__c, p.certRedDT__c
                                 FROM Pocket__c p 
                                 FOR UPDATE ];
       
               
       for (Pocket__c pcT : pKs) {
            if (lineItemPrice<=pcT.Pocket_Balance__c) {
             
             pcT.Pocket_Balance__c     -= lineItemPrice;                  // Pocket Balance is deducted.
             pcT.certRed_Ref__c         = sList.rId4_Ref__c;              // Ref to Certificate being sourced.
             pcT.certRedValue__c        = lineItemPrice;                  // Value substracted from Balance.
             pcT.certRedDT__c           = dateTime.now();                 // Date & Time of withdrawal.
                                       
               
             lrdm.Certificate__c        = sList.rId4_Ref__c;          // Ref to Certificate being sourced.
             lrdm.Pocket__c             = pcT.id;                            // Ref to Pocket just used to source certificate.
             lrdm.Points__c             = lineItemPrice;                  // Amount withdrawn from Pocket Balance.
             lrdm.Redem_Date__c         = dateTime.now();       // Date & Time of withdrawal.
             if (pcT.Pocket_Balance__c==0) {
             lrdm.Total_use_of_Pocket__c = true;  }                   // Pocket Balance is zero, so Total use of Pocket Flag is TRUE.
             else  {lrdm.Total_use_of_Pocket__c = false; }         // Pocket Balance is greater than zero.
             
             RDML.add(i++, lrdm);                                               // Add lrdm record to RDML List. 
             break;                                                                        // Certificate has been fully sourced we may stop loop.
            }
            else {
             lineItemPrice         -= pcT.Pocket_Balance__c;           // Pocket Balance is deducted from Certificate Balance.
             pcT.certRedValue__c    = pcT.Pocket_Balance__c;   // Value substracted from Balance.
             pcT.Pocket_Balance__c  = 0;                                      // Pocket Balance is zeroed.
             pcT.certRed_Ref__c     = sList.rId4_Ref__c;               // Ref to Certificate being sourced.
             pcT.certRedDT__c       = dateTime.now();                   // Date & Time of withdrawal.
             
             lrdm.clear();                                                // Rdm Line record initialized.
             lrdm.Certificate__c        = sList.rId4_Ref__c;              // Ref to Certificate being sourced.
             lrdm.Pocket__c             = pcT.id;                         // Ref to Pocket just used to source certificate.
             lrdm.Points__c             = pcT.certRedValue__c;            // Amount withdrawn from Pocket Balance.
             lrdm.Redem_Date__c         = dateTime.now();                 // Date & Time of withdrawal.
             if (pcT.Pocket_Balance__c==0) {
             lrdm.Total_use_of_Pocket__c = true;  }                       // Pocket Balance is zero, so Total use of Pocket Flag is TRUE.
             else  {lrdm.Total_use_of_Pocket__c = false; }                // Pocket Balance is greater than zero.
             RDML.add(i++, lrdm);                                         // Add lrdm record to RDML List.
                         
            }
       } // for ends here ...
       
       if (lineItemPrice==0 || (sList.rCheckBox3__c & sList.rCurrency3__c*sList.rNumber2__c>lineItemPrice )) {
           update pKs;                                                      // Pocket records being updated.
           insert RDML;                                                     // Rdm_Line records are inserted.
           }
       }
       else {
          rList.rOK__c      = false;                  // Wallets & Pockets found. Certificate Line WAS NOT sourced; Insuficient Funds.
         
       }
 } 

 

Best !

sfdcfoxsfdcfox
Just take out the i++, it's not necessary.
EUSEUS

Hi sfdcfox,

 

I tried it. It does not give the index error, however it fails at the insert where it tells that there are duplicate errors on the list.

I followed the execution on the developer console and for some reason I don't see the last record processed overwrites the previous records and all 3 records on the list get all the same with the information of the last one ....

Any idea why the last record overwrites the previous two?