0

I need to display related object records dynamically on VF page but getting only last object records from list of sobject which I am using in ape class.Here is my code:

Apex code:

public class FindRelatedData {

public String getQueries() {
    return null;
}
public Id id = '0016F00001rZlrM';
string objType;
string childName;
string fname;
//string qry;

Public List fieldlist{get;set;} Public List objQry{get;set;} List queries; Map fmap = new Map(); List objNames = new List(); public List childObjs = new List(); public Map> objFieldsMap {get;set;} public Map> objRecordsMap{get;set;} public List getColumns() { return new List; } public FindRelatedData() {

    objFieldsMap = new Map<String, List<string>>();
    objRecordsMap = new Map<String, List<sObject>>();
    objType = id.getsobjecttype().getDescribe().getName();
    System.debug('object Type:'+objType);
    Schema.DescribeSObjectResult relationType = Schema.getGlobalDescribe().get(objType).getDescribe();
    for(Schema.ChildRelationship childs: relationType.getChildRelationships())
    {
        System.debug('childs:'+childs);

        Schema.SObjectType sobj = childs.getChildSObject();
        System.debug('sobjects:'+sobj);          
        childName = String.valueOf(childs.getChildSObject());
        system.debug('Child Object:'+childName);
        List<string> setFlds = new List<String>();
        if(childName == 'SP001_MyDev__Account__c' || childName == 'opportunity' ) 
        {
            Schema.SobjectField fields = childs.getField();
            fname = childs.getField().getDescribe().getName();
            System.debug(fname);
            //System.debug('childName:'+childName);
            sObject obj = Schema.getGlobalDescribe().get(childName).newSObject();
            System.debug('objs:'+obj);
            objNames.add(childName);
            childObjs.add(obj);
            fieldlist = new List<string>();
            fmap = childs.getChildSObject().getDescribe().fields.getMap();
            System.debug('Fields*****'+fmap);
            for(String f:fmap.keySet())
            {
                System.debug('field name:'+f);

                fieldlist.add(f);
            }
            objFieldsMap.put(childName,fieldlist);
            System.debug('objFields***'+objFieldsMap);
            objQry = executeqry(fieldlist);
            System.debug('child objects:'+objQry);
            objRecordsMap.put(childName,queries);
                System.debug('records:'+objRecordsMap);
        }

    }
}


Public list<sObject> executeqry(List<string> fieldlists)
{


           string qry = fieldlists.get(0);
                for(Integer i =1;i<fieldlists.size();i++)
                {
                    qry  = qry+ ' , ' + fieldlists.get(i) ;
                    System.debug('Qry----'+qry);
                }
                 System.debug('printquery:'+' SELECT '+''+ qry +' '+' FROM '+' '+ childName +' '+' WHERE '+''+fname+ ' = \'' +id+'\''+ ' LIMIT '+' '+'3');
                queries = Database.query(' SELECT '+''+ qry +' '+' FROM '+' '+ childName +' '+' WHERE '+''+fname+ ' = \'' +id+'\''+ ' LIMIT '+' '+'3');
                System.debug('Query:'+ queries); 
            //executeqry(fieldlist);

                //fieldlist.addAll(fieldlists);

    System.debug('fieldlists:'+fieldlist);
    return queries;
}

}

VF Page:

<apex:page controller="FindRelatedData" >

<apex:repeat value="{!objFieldsMap}" var="obj">
    {!obj}<br></br>
  <apex:repeat value="{!objRecordsMap[obj]}" var="record">

        <apex:repeat value="{!objFieldsMap[obj]}" var="field">
            {!record[field]}<br></br>
                   <!---<apex:column headerValue="{!field}">
                      <apex:outputText value="{!record[field]}"/>
                   </apex:column>-->
        </apex:repeat>
  </apex:repeat>

</apex:repeat>

</apex:pageBlock>  
</apex:form> 

Here I am getting only opportunity records but in debug log it returns for contact and opportunity both.WHere I am doing wrong?

  • Whats the debug of this line? System.debug('child objects:'+objQry); – Pranay Jaiswal Nov 22 '18 at 10:22
  • List of Contact records then opportunity records – sivanee Nov 22 '18 at 10:24
  • Do I need to use wrapper class? – sivanee Nov 22 '18 at 10:25
  • It is because of fieldlist = new List<string>();. If you will check on visualforce page you will find that you only have opportunity fields in this list. – Pragati Jain Nov 22 '18 at 10:27
  • then what should I do to get those field list for every object in visualforce page? – sivanee Nov 22 '18 at 10:33
0

For this you have to create two Maps:

1.) Create a Map like Map<String, List<string>> objFieldsMap where the key is SObject name (Opportunity or Contact) and value part should contain the corresponding List of fields.

2.) Create another Map like Map<String, List<sObject>> objRecordsMap where the key is SObject name (Opportunity or Contact) and value part should contain the corresponding List of sobject records.

Then iterate these Maps in visualforce page like:

<apex:page controller="FindRelatedData" >
  <apex:form >
   <apex:pageBlock title="Related Data">

   <apex:pageblockTable value="{!objFieldsMap}" var="obj">

      <apex:repeat value="{!objRecordsMap[obj]}" var="record">
            <apex:repeat value="{!objFieldsMap[obj]}" var="field">
               <apex:column headerValue="{!field}">
                  <apex:outputText value="{!record[field]}"/>
               </apex:column>
            </apex:repeat>
      </apex:repeat>

    </apex:pageblockTable>

  </apex:pageBlock>  
  </apex:form> 
  </apex:page>
  • Thanks for your response @Pragati Jain.It's showing this error: Map key null not found in map Error is in expression '{!objRecordsMap[obj]}' in component <apex:pageBlockTable> . – sivanee Nov 22 '18 at 12:11
  • I can help you if you will share your updated code here – Pragati Jain Nov 22 '18 at 12:30
  • error is only showing for pageblockTable ,when I used repeat instead of pgblocktable it's working but i need to use pageblockTable.I will share my updated code. – sivanee Nov 22 '18 at 12:40
  • I have updated my code in question.@ Pragati Jain – sivanee Nov 26 '18 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.