1

I am checking for products duplicated in my webservice but it is showing a "List Out of Bounds of 1" error in the below code.

     **for(integer j=0.prodsj++)** // Error Occur Point
      {

      {
        p = new  Product2(isActive=true);
      }
         p.Name =pName;         
            p.productCode = pCode;
            pList.add(p); 
      }


         Database.upsert(pList);   

        for(integer j=0;j<req.accounts[i].qline.size();j++)

       {

2 Answers 2

1

Not being able to see the whole code, my advice would be to start by replacing the "CHECK THE LOOP ONCE" code with the following where I have replaced the re-use of the i variable to see if that resolves the bounds error at least.

for( integer x = 0;x< req.accounts.size(); x++) 
{ //  CHECK THE lOOP ONCE                      

    for(integer j=0;j<req.accounts[x].prods.size();j++)
    {
        pCodeSet.add(req.accounts[x].prods[j].pCode);
    }
} 

When this piece of code is finished the i variable will be in the state it was before entering the code because we haven't modified it with the for loop.

Assuming there is nothing else putting the i variable outside the bounds of the list, future use of the i variable in the code should not cause the bounds error.

ie

…
if(existpccMap.containsKey(req.accounts[i].prods[j].pCode))  // now safe

I hope this helps.

6
  • shall i share my whole code.
    – Sathya
    Commented Apr 5, 2014 at 8:30
  • @Sathya - Does the change I suggested remove the 'List Out of Bounds of 1' Error? Commented Apr 5, 2014 at 8:40
  • I chage my code like this it is work for multiple records insert.
    – Sathya
    Commented Apr 5, 2014 at 8:43
  • now i am not getting any error but i have a doubt my code works for multiple products insert or not..?
    – Sathya
    Commented Apr 5, 2014 at 8:47
  • One issue you may have with multiple new products is if you add two with the same product code that don't exist already so are not in the existpccMap map. Where you create a new Product2 you should add it to the existpccMap map so if another new product comes along it can use the one you just created. It sounds like you've solved the question you asked and I think we're going a bit beyond the scope of the original question. Commented Apr 5, 2014 at 8:58
3
for( i = 0;i< req.accounts.size(); i++){ //  CHECK THE lOOP ONCE

     for(integer j=0;j<req.accounts[i].prods.size();j++)
     {
        pCodeSet.add(req.accounts[i].prods[j].pCode);
     }
}

Above for loop is updating the variable i of your main for loop and after this for loop i will have the value of req.accounts.size(). Then in the next for loop since you are using the same i it's throwing the exception. Reset it or manage it according to your business.

**for(integer j=0;j<req.accounts[i].prods.size();j++)** // Error point
      {
      if(existpccMap.containsKey(req.accounts[i].prods[j].pCode))
...
9
  • i am inside the main for loop or shall i share my whole code.may i know where i want to change my code.
    – Sathya
    Commented Apr 4, 2014 at 8:03
  • Yes it'd be better. Edit the question and add it(point where the exception occurs as you currently doing). According to the posted code you are outside of the top for loop.
    – highfive
    Commented Apr 4, 2014 at 8:07
  • i update my code can you check it once. And I add the point where i am getting an error.
    – Sathya
    Commented Apr 4, 2014 at 8:13
  • i posted my updated code can you check it once.
    – Sathya
    Commented Apr 4, 2014 at 8:23
  • Still the same issue there. One before for loop(not the loop having the query) is updating the variable i and so all the below codes refer that updated value, which tend to throw an out of bound exception.
    – highfive
    Commented Apr 4, 2014 at 8:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .