System.IndexOutOfRangeException: Index was outside the bounds of the array

696    Asked by CarolineBrown in Salesforce , Asked on Aug 5, 2021
 I am trying to fetch all the attachments for a record from salesforce by .Net webservice.

Below is the code which I am was used. When I was fetching a single attachment, didn’t faced any issue. But when I tried to fetch multiple attachment,it is giving me  the below error -

'System.IndexOutOfRangeException: Index was outside the bounds of the array. at WcfService1.Service1.ConnectSalesforce() in C:UsersmohapatssourcereposWcfService1WcfService1Service1.svc.cs:line 149 at WcfService1.Service1.CombineMultiplePDFs(List`1 InFiles, String OutFile) in C:UsersmohapatssourcereposWcfService1WcfService1Service1.svc.cs:line 59'
c# code -
if(currentLoginResult.sessionId != null) { QueryResult queryResult = null; String SOQL = "SELECT Id FROM Attachment where ParentId = '0010v0000071CHp'"; queryResult = sfdcBinding.query(SOQL); List ids = new List(); if (queryResult.size > 0) { for(int i = 0; i < queryResult xss=removed> 0) { //QueryResult queryResult1 = null; //String SOQL1 = "SELECT Id,Body,Name FROM Attachment where Id = '" + ids + "'"; //queryResult1 = sfdcBinding.query(SOQL1); String query = "SELECT Id,Body,Name FROM Attachment "; String strIds = ""; foreach (String id in ids) { if (strIds.Equals("")) { strIds = "'" + id + "'"; } else { strIds += ",'" + id + "'"; } } query += "where id in (" + strIds + ")"; QueryResult result = sfdcBinding.query(query); Boolean done = false; if (result.size > 0) { while (!done) { sObject[] records = result.records; for (int i = 0; i < records xss=removed xss=removed xss=removed>

The below soql -

QueryResult result = sfdcBinding.query(query);

This should return two arrays

result.records[0] & result.records[1]

but it is returning only one

result.records[0]

but result.size is showing as 2

So at the below line

Attachment q = (Attachment)result.records[i];

when it comes for the 2nd time, it throws the above error.

 

Kindly help how can I resolve this  system.indexoutofrangeexception: 'index was outside the bounds of the array ‘ error.’


Answered by Emily Coleman

You've run into the query result paging where the size of the SOQL query result size will be greater than the number of records returned in the current QueryResult.

When dealing with Attachments it's not uncommon for the QueryResult to only have a single record.

The query result object contains up to 500 rows of data by default. If the query results exceed 500 rows, then the client application uses the queryMore() call and a server-side cursor to retrieve additional rows in 500-row chunks. You can increase the default size up to 2,000 in the QueryOptions header. For more details see Change the Batch Size in Queries in the Force.com SOQL and SOSL Reference. [Source]

You need to check if done is true. If not, use the queryLocator to fetch the next lot of results until done turns false.

 This will resolve your issue.



Your Answer

Interviews

Parent Categories