function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Invalid Cast Problem

I am querying for a list of Opportunities based upon lastmodifieddate and a couple of other fields, I would like to return an array of Opportunities from my function. I can get the data but when I try to cast the resulting array into an array of Opportunities I get the "Specified Cast is Invalid" error. I have tried updating the WSDL with no joy.

Any help is greatly appreciated.

My Code:

private Opportunity[] GetUpdatedClosedOpportunities(SqlDateTime startTime, SqlDateTime endTime)
Opportunity[] thegoods = null;

QueryResult qr = null; //storage location for returned data
binding.QueryOptionsValue = new sforce.QueryOptions(); //define the query specifications
binding.QueryOptionsValue.batchSize = 450; //number of records to return.
binding.QueryOptionsValue.batchSizeSpecified = true; //specify that the above item is supplied

string startTimeString = "2005-12-15T21:43:31.000Z"; //temporarily hard coded for testing
string sqlStatement = "SELECT id, name FROM Opportunity WHERE isclosed = TRUE AND lastmodifieddate > " + startTimeString;
qr = binding.query(sqlStatement);
thegoods = (Opportunity[]) qr.records; //Cast Invalid error here!!

return thegoods;
catch (Exception ex)
Console.WriteLine ("Failed to get Opportunity: " + ex.Message);
return thegoods;

Message Edited by JonSimmons on 01-05-2006 07:20 AM

For some reason in .NET you can't cast an array of baseTypes into an array of concreteTypes (make sense i guess, not all the items in the array might not be of the same concreteType).

You'll have to walk the array to create an Opportunity array.
Crud, I was hopeing to avoid that.
Seems odd to me really, I would think that .NET could just throw and exception if the conversion failed, doesn't seem like Microsoft to avoid a potential problem as minor as that and limit the capabilities of the language so drasticly. I would expect MS to make it up to me to ensure that the conversion is going to go correctly and if not throw and appropriate exception. Oh well.