4

I have a list of sObject which get populated dynamically all the objects and the related fields. The list look like this on debug

(Contact:{OtherCountry=, Division_Type__c=}, Account:{AccountNumber=})

I want to loop through the list of contact and Account.
E.g. : if loop through contact then get all the fields related to it and display as <inputfield /> on VF page. Do anyone have any idea.

2 Answers 2

2

If you want to loop over the fields there's a few options here. You can use a FieldSet:

<apex:repeat value="{!$ObjectType['Account'].FieldSet.MyFieldSet}" var="field">
    <apex:outputText value="{!field}" />
</apex:repeat>

Or rather than sending a List<SObject> down to the page, try List<Map<String,Object>>

<apex:repeat value="{!ListOfMaps}" var="map">
    <apex:repeat value="map" var="key">
        <apex:outputText value="{!map[key]}" />
    </apex:repeat>
</apex:repeat>

In the Apex you might marshall the list of objects into the list of maps with serialization:

List<Object> ListOfMaps;
ListOfMaps = (List<Object>)Json.deserializeUntyped(Json.serialize(listOfObjects));
0

The instanceof keyword allows you to check the if an object is an instance of a class. Thus you could have something which is passed in as simply an Object type, and you can then determine if it is an instance of your custom class and take appropriate action, e.g.

Here is what you need to do:

  1. loop over the list
  2. use the instance of keyword on each sOject to check if it is of type Contact or Account
  3. if the instance is of type contact then get contact related fields
  4. if the instance is of type account then get the account related fields

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .