• Jason Smith 23
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
How come I can't dynamically cast a QueryResult to an sObject and view the values without have to cast the result to a difinitive sObject? I've been able to view returned values from a query within Microsoft Access, loop through the queryresult and then insert record values into the appropriate tables without having to specifiy whether it is an Account or Contact, or Lead, etc.

Instead of having to do something like:
sObject s = new Account();
Account a = Account(s).

Why can't I just loop through the sObject records and retrieve the values?

I'm working on an integration piece and the sObject type has to be dynamic. I cannot cast it to a specific sobject every single time I need to retrieve records.

try
                    {
                        QueryResult qResult = sfProd.query(strSelectStatement);
                        sObject[] s = qResult.records;
                       
                        Boolean done = false;

                        if (qResult.size > 0)
                        {
                            while (!done)
                            {
                                // foreach statement cannot operate on variables of type QueryResult because QueryResult does not contain a public definition for GetEnumerator
                                //foreach (QueryResult q in qResult)
                                //{
                                //}
                               
                                for (Int32 j = 0; j < qResult.records.Length; j++)
                                {
                                    MessageBox.Show(s[j].GetType().ToString());
                                    MessageBox.Show(s[j].GetType().Name);
                                    //MessageBox.Show(s[j].GetType().GetFields("Name").ToString());
                                    MessageBox.Show(s[j].Id.ToString());
                                }

                                if (qResult.done)
                                {
                                    done = true;
                                }
                                else
                                {
                                    qResult = sfProd.queryMore(qResult.queryLocator);
                                }
                               
                            }
                        }
                    }
                    catch (SoapException e)
                    {
                        MessageBox.Show(e.Message);
                    }