I'm working on a SOAP integration (.NET C#) to Salesforce and I am facing a problem when I try to get results from Salesforce.
To do this I'm using the next example code of the Salesforce walkthrough:
private void querySample()
{
String soqlQuery = "SELECT FirstName FROM Account";
QueryResult qr = binding.query(soqlQuery);
if (qr.size > 0)
{
while (!done)
{
sObject[] records = qr.records;
for (int i = 0; i < records.Length; i++)
{
Account con = (Account)records[i];
}
if (qr.done)
{
done = true;
}
else
{
qr = binding.queryMore(qr.queryLocator);
}
}
}
else
{
Console.WriteLine("No records found.");
}
}
catch (Exception ex)
{
Console.WriteLine("\nFailed to execute query succesfully," +
"error message was: \n{0}", ex.Message);
}
The problem is in
Account con = (Account)records[i];
When it tries to cast the sObject to the Account I get a runtime error:
System.InvalidCastException: 'An object of type' SalesForceIn.SForce.sObject 'cannot be converted to type' SalesForceIn.SForce.Account '.'
Why I am getting this error? Do I need to create my own converter? Maybe be it's related to wsdl problem?
Thanks in advance. Regards.
Edit: @sfdcfox I'm using Enterprise WSDL because when I export the Partner I don't have the entities like Account in the Wsdl definition. Thank you.
@Keith C Yes I have the correct using in both. Thank you.
using
setup i.e. the equivalent ofimport com.sforce.soap.enterprise.sobject.Account
?