• Debd89
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

I was preparing this page where I need to Show All fields of Account object and some selected fields from that list Should only should be queried from the Account object. All is resolved till now that I face one major hurdle. Now I need to represnt this Dynamically Queried Account List in a table or Grid in the page itself. The problem lies in the fact that the queried columns names are not known to me but in a " Set<String> rightvalues " so the <apex:pageBlockTable value="{!accList}" var=" dispAcc"> <apex:column value="{!dispAcc.?}> .....How do I resolve this?

 

my code is as given under this following

 

The Controller :

public with sharing class GridRep {

public list<Account> accList = new list<Account>();
public list<Account> getAccList()
{
return accList;
}
// public list<Account> accList{get;set;}
public list<String> availableFields{get;set;}
public list<String> selectedFields {get;set;}
public Set<String> leftvalues = new Set<String>();
public Set<String> rightvalues=new Set<String>();
public String strQuery;

public GridRep(){
availableFields = new List<String>();
selectedFields = new List<String>();

}
public List<String> getFieldList(Map<String, Schema.SObjectField> fieldMap)
{
//Make a comma-separated list of the fields
List<String> fieldList =new List<String>();
for(String fieldName:fieldMap.keySet())
{
Schema.DescribeFieldResult fieldDescribe = fieldMap.get(fieldName).getDescribe();
fieldList.add(fieldDescribe.getName());

}
System.debug(fieldList);
return fieldList;
}


public PageReference refreshPage()
{
availableFields = new List<String>();
Map<String, Schema.SObjectField> fieldMap=new Map<String, Schema.SObjectField>();
fieldMap = Schema.SObjectType.Account.fields.getMap();
List<String> fieldNames=getFieldList(fieldMap);
fieldNames.sort();
for (String fldName:fieldNames)
{
availableFields.add(fldName);
leftvalues.add(fldName);
}

return null;
}

public List<SelectOption> getunSelectedFields()
{
List<SelectOption> optionFields = new List<SelectOption>();
List<string> tempList = new List<String>();
tempList.addAll(leftvalues);
tempList.sort();
for(string s : tempList)
optionFields.add(new SelectOption(s,s));
return optionFields;
}
public PageReference selectclick()
{
selectedFields.clear();
for(String s : availableFields)
{
leftvalues.remove(s);
rightvalues.add(s);
}
return null;
}
public List<SelectOption> getSelectedValues()
{
List<SelectOption> optionsFields2 = new List<SelectOption>();
List<string> tempList2 = new List<String>();
tempList2.addAll(rightvalues);
tempList2.sort();
for(String s : tempList2)
optionsFields2.add(new SelectOption(s,s));
return optionsFields2;
}

public PageReference unselectclick()
{

for(String s : selectedFields)
{
rightvalues.remove(s);
leftvalues.add(s);

}
return null;
}

public pagereference find()
{
strQuery='Select ';
for(String temp:rightvalues)
{
strQuery+=temp+',';
}
strQuery=strQuery.removeEnd(',');
strQuery+=' from Account';

accList=new list<Account>();
try
{
accList=Database.query(strQuery);
}catch(Exception e)
{
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Error in Database Fetch');
ApexPages.addMessage(errormsg);
}

return null;
}


}

 

and the page as is :

 

<apex:page controller="GridRep" sidebar="false" >
<apex:form id="fm1">
<apex:sectionHeader title="Grid Like Representation"/>
<apex:commandButton action="{!refreshPage}" value="Populate Available"/>
<apex:pageBlock >
<apex:pageBlockButtons location="top">
<apex:commandButton value="Find" action="{!find}" id="button1"/>

</apex:pageBlockButtons>
<apex:panelGrid columns="3" id="abcd">
<apex:selectList value="{!availableFields}" multiselect="true" style="height:80px;width:150px" >
<apex:selectOptions value="{!unSelectedFields}" />
</apex:selectList>
<apex:panelGroup >
<apex:commandButton value="Transfer Right" action="{!selectclick}" reRender="abcd"/>
<apex:commandButton value="Transfer Right" action="{!unselectclick}" reRender="abcd"/>
</apex:panelGroup>
<apex:selectList value="{!selectedFields}" multiselect="true" style="height:80px;width:150px" >
<apex:selectOptions value="{!SelectedValues}" />
</apex:selectList>
</apex:panelGrid>
<apex:pageBlockSection >
<!--   Here Should Be the code to Display Dynamic queried list accList -->

</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

  • January 25, 2013
  • Like
  • 0

hi,

 

I have displayed all the object names in a select list and while an object is selected, i am retrieving the object name and i have to select the field names for it. For this i found that the syntax as given below,

 

Schema.DescribeFieldResult f = Schema.sObjectType.Account.fields;

 

The Account object is hardcoded here, but, in my scenario, object name is dynamically changing, how to modify this syntax into dynamically changing one. any ideas or any other syntax's available to get field names?? 

Or is there any soql query to retrieve the field for the requested object? please provide any ideas or suggestions to retrieve the field names...

 

thanks,

abivenkat,

SFDC Learner