1

I am working with webservice at below line i am getting error

System.ListException: List index out of bounds: 0

       res.resName = 'success';
       Database.SaveResult[] srList = Database.insert(accList, false);
       for(integer i = 0;i< req.accounts.size(); i++){
         if(srList[i].id !=null){
          for(integer j=0;j<req.accounts[i].contacts.size();j++)
          {
            con = new Contact();
            con.LastName = req.accounts[i].contacts[j].clName;
            con.FirstName =req.accounts[i].contacts[j].cfName; 
            con.Email = req.accounts[i].contacts[j].email;
            con.MobilePhone = req.accounts[i].contacts[j].mobile;             
            con.accountId = srList[i].id;
            contactList.add(con);  
          } 
           insert contactList;
        }
       }
       return res;

     }
  • Are you sure the req.accounts list is the same as accList? If those 2 don't match up, I could see where an error would pop up. If you can post any code from above that to help shed some light on how they interact, it would be helpful. – Michael Welburn Dec 26 '13 at 15:30
  • @MichaelWelburn i update my code once check it. – Sathya Dec 26 '13 at 15:34
  • @Sathya I've updated the tags. Can you please, in future questions try to select relevant tags ? Nothing in your question points to any relevance with SOAP API or webservices. – Samuel De Rycke Dec 26 '13 at 15:41
1

Per your code above, only a subset of the req.accounts records actually get added to the accList (in the ELSE statement).

As such, you need to modify the for loop at the end to use a List that mirrors accList but contains the AccountWrapper objects instead of Accounts (in order to access the Contact records on that custom APEX class).

EDIT: Updated answer based on Chat.

  • could you updaye my code, i am confusion where i have to modify. – Sathya Dec 26 '13 at 15:40
  • Everything after your Database.insert() needs to refer to accList instead of req.accounts. Because you only want to modify accounts that were successfully inserted, you will need to use the collection that you actually inserted (accList) rather than the original collection (req.accounts) that potentially had additional records that were not inserted. – Michael Welburn Dec 26 '13 at 15:42
  • that means i have to change my for loop for(integer i = 0;i< accList.size(); i++){ – Sathya Dec 26 '13 at 15:44
  • Yes, you'll need to change that but also all the references to req.accounts inside that for loop (the other for loop, and all the references while setting field values. Does it make sense that if the accList has 8 of the 10 values in req.accounts, that if you try to leverage req.accounts inside that loop that you will be setting the wrong values sometimes? – Michael Welburn Dec 26 '13 at 15:47
  • i update my code like this but i am getting an error for(integer i = 0;i< accList.size(); i++){ if(srList[i].id !=null){ for(integer j=0;j<accList[i].contacts.size();j++) { con = new Contact(); con.LastName = accList[i].contacts[j].clName; con.FirstName =accList[i].contacts[j].cfName; con.Email = accList[i].contacts[j].email; con.MobilePhone = accList[i].contacts[j].mobile; con.accountId = srList[i].id; contactList.add(con); } insert contactList; – Sathya Dec 26 '13 at 15:50
0

Simply change it to:

res.resName = 'success';
   Database.SaveResult[] srList = Database.insert(accList, false);
   for(integer i = 0;i< srList.size(); i++){
     if(srList[i].id !=null){
      for(integer j=0;j<req.accounts[i].contacts.size();j++)
      {
        con = new Contact();
        con.LastName = req.accounts[i].contacts[j].clName;
        con.FirstName =req.accounts[i].contacts[j].cfName; 
        con.Email = req.accounts[i].contacts[j].email;
        con.MobilePhone = req.accounts[i].contacts[j].mobile;             
        con.accountId = srList[i].id;
        contactList.add(con);  
      } 
       insert contactList;
    }
   }
   return res;

 }
  • The issue in the code is req.accounts potentially having more values than accList/srList. – Michael Welburn Dec 29 '13 at 15:12
  • If that is the issue then you will NOT be able to use the index values for either srList or the req.accounts as they will not match up. In order to use the index's the lists have to be the same size. Also, if that is the case then that should be brought up in a different question as it is a completely separate issue that must be worked out. – Eric Dec 31 '13 at 3:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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