I am trying to fetch all the attachments for a record from salesforce by .Net webservice.
Below is the code which i am trying to do. If i am fetching a single attachment, i am not facing any issue. But if there are multiple attachment,i am unable to fetch and i am getting the below error -
'System.IndexOutOfRangeException: Index was outside the bounds of the array.
at WcfService1.Service1.ConnectSalesforce() in C:\Users\mohapats\source\repos\WcfService1\WcfService1\Service1.svc.cs:line 149
at WcfService1.Service1.CombineMultiplePDFs(List`1 InFiles, String OutFile) in C:\Users\mohapats\source\repos\WcfService1\WcfService1\Service1.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<String> ids = new List<String>();
if (queryResult.size > 0)
{
for(int i = 0; i < queryResult.size; i++) {
Attachment readAttatchmentId = (Attachment)queryResult.records[i];
ids.Add(readAttatchmentId.Id);
}
}
if(ids.Count > 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.Length; ++i)
{
Attachment q = (Attachment)records[i];
}
if (result.done)
{
done = true;
}
else
{
result = sfdcBinding.queryMore(result.queryLocator);
}
}
}
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.