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
megha naikmegha naik 

Dynamically cast QueryResult to generic type and view the values returned in the QueryResult without having to specify an actual class type such as Case or Contact?

I want to use Enterprise API and dynamically cast QueryResult to SObject/Object and get values for all fields without specifying the class / field name like Case,ID etc. Currently i am using reflection to cast QueryResult return to Object type and then dynamically invoke setters for all the fields. I am looking at a cleaner solution.
This is what i am trying to do
 for (int i=0;i<queryResults.getRecords().length;i++) {
          Object tableObject = (Object)queryResults.getRecords()[i];
          System.out.println(tableObject.getClass());       
         DescribeSObjectResult[] dsrArray = connection
                    .describeSObjects(new String[] { "Contact" });
            DescribeSObjectResult dsr = dsrArray[0];
            for (int j = 0; j < dsr.getFields().length; j++) {
                Field field = dsr.getFields()[j];
                String value = getValueFromObject(tableObject, field.getName()); // dynamically invokes getters
                System.out.println(" "+field.getName() + " Label: "
                        + field.getLabel() + " Type: " + field.getType() + " Value: "+ value);
                writer.write(" "+field.getName() + " : "+ value);
            }
            writer.write("\n");