• megha naik
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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");

Hi,

    Is there way to iterate through the fields of Object returned through the QueryResult and get the name/value of the fields.

 Currently the only way I could see is to get to the fields is the get set methods  for that particular field. Which implies that I need to know what fields are there in the returned object, which is file if I am writing the query in the code and I know what fields will be returned. But if I am makinga tool to allow a user to specifiy the query and the tool generates an XML out of the returned objects then a generic method to loop through the fields will be very helpful. I think the previous API since every thing was loaded in a hash map, therefore it was much easier to do what I am trying to do.

Thanks

fruser

  • December 15, 2003
  • Like
  • 0