You need to sign in to do that
Don't have an account?

Displaying Object fields
Hi all,
I have a custom object that has a few fields - 3 picklists and 2 texts.
I want to display the fields the objects fields like we see in the custom report filter.
Can anyone point me on how can i do it?
Thanks much in advance!
accountFields = Schema.SObjectType.Account.fields.getMap();
Set<string> flds = new Set<String>();
flds = accountFields.keySet();
List<SelectOption> options = new List<SelectOption>();
for(string f : flds)
{
string fldLabel = accountFields.get(f).getDescribe().getLabel();
options.add(new selectOption(f,fldLabel));
}
Thanks for the reply. But this is not what i am looking for. I have created a custom object called Scoring Rule. The custom object has 5 fields - 3 picklists (Operator1, Operator2, Score) and 2 text fields (Value1, Value2). I want to display these in a table 5 times (like we see in the custom reports filter). Can you please tell me that how can i do that? I am a newbie to Salesforce Development. Please help me!!!!
I think we can use <table> tag:
Example:
<table style="width: 632px">
<tr>
<td> <apex:inputField value="{!opportunity.name}"/>
</td>
<td> <apex:inputField value="{!opportunity.amount}"/>
</td>
<td>
</td>
</tr>
<tr>
<td> <apex:inputField value="{!opportunity.name}"/>
</td>
<td><apex:inputField value="{!opportunity.amount}"/>
</td>
<td>
</td>
</tr>
<tr>
<td> <apex:inputField value="{!opportunity.name}"/>
</td>
<td><apex:inputField value="{!opportunity.amount}"/>
</td>
<td>
</td>
</tr>
<tr>
<td> <apex:inputField value="{!opportunity.name}"/>
</td>
<td><apex:inputField value="{!opportunity.amount}"/>
</td>
<td>
</td>
</tr>
<tr>
<td> <apex:inputField value="{!opportunity.name}"/>
</td>
<td><apex:inputField value="{!opportunity.amount}"/>
</td>
<td>
</td>
</tr>
</table>
No that didnt work. I have an object ScoringRule__c. I want to display something like this:
Field Operator1(Picklist) Value1(Textfield) Operator2(Picklist) Value2
Annual Revenue Greaterthan/Lessthan/Equalto Greaterthan/Lessthan/Equalto
All my fields are in the Object - ScoringRule_Operator1__c, ScoringRule_Val1__c etc.
I want to display the same row 5 times in a table.
Following is the snippet of my visualforce code-
<apex:pageBlockTable value="{!ScoringRule}" var="sr" id="theTable" rowClasses="odd,even"
styleClass="tableClass" rows="5">
<apex:column width="25%">
<apex:facet name="header">Rule 1</apex:facet>
<apex:inputField value="{!sr.ScoringRule_Rule1__c}"/>
</apex:column>
<apex:column width="25%">
<apex:facet name="header">Value 1</apex:facet>
<apex:inputText value="{!sr.ScoringRule_Val1__c}"/>
</apex:column> ..........
The problem is that it doesnt display anything. Can anyone tell me what am i doing wrong?
Hi Mukul,
This is a visualforce question, you may get more help on the Visualforce boards.
from the looks of it, your controller is not returnining a list of objects.
Look in your controller for the method getScoringRule()
and add some debug statements to ensure that you have a list of records.
if you paste a simple controller and page , using the SRC Code button, over on the Visualforce boards,
we'll try to help
have you built a simple visualforce account page yet?
suggest you try that first and then move on to custom objects.
Hi Ron,
Here is my Controller Code:
public class newScoreRuleController { Lead lead; // public List<ScoringRule__c> criteriaLine = New List<ScoringRule__c>(); public List<ScoringRule__c> ScoringRule { get; private set;} //public List<ScoringRule__c> ScoringRule; public Lead getLead() { if(lead == null) lead = new Lead(); return lead; } public String getFieldName() { String s = ApexPages.currentPage().getParameters().get('fld'); return s; } public List<SelectOption> GetFieldsForObject(Map<String, Schema.SObjectField> objFields, string lblPrefix, string fldPrefix) { // Build a list of field names to use to iterate the Map of field object pointers Set <string> flds = new Set <String>(); flds = objFields.keySet(); // Add the keyset of field names to a list so that it can be sorted List<String> fldList = new List<String>(); for (string f : flds) { fldList.add(f); } fldList.sort(); List<SelectOption> options = new List<SelectOption>(); for (string f : fldList) { string fldName = objFields.get(f).getDescribe().getName(); string fldLabel = objFields.get(f).getDescribe().getLabel(); string fldType = ('' + objFields.get(f).getDescribe().getType()).replace('Schema.DisplayType.', '') ; if (fldType <> 'REFERENCE' && fldType <> 'ID' && fldName <> 'IsDeleted' && fldName <> 'SystemModstamp') options.add(new selectOption(fldType + '/' + fldPrefix + fldName, lblPrefix + fldLabel )); if (fldName == 'OwnerID') { options.add(new selectOption('STRING/' + fldPrefix + 'Owner.Name', lblPrefix + 'Owner.Name')); options.add(new selectOption('STRING/' + fldPrefix + 'Owner.Alias', lblPrefix + 'Owner.Alias')); options.add(new selectOption('STRING/' + fldPrefix + 'Owner.Department', lblPrefix + 'Owner.Department')); options.add(new selectOption('STRING/' + fldPrefix + 'Owner.Division', lblPrefix + 'Owner.Division')); } else if (fldName == 'LastModifiedByID') { options.add(new selectOption('STRING/' + fldPrefix + 'LastModifiedBy.Name', lblPrefix + 'LastModifiedBy.Name')); options.add(new selectOption('STRING/' + fldPrefix + 'LastModifiedBy.Alias', lblPrefix + 'LastModifiedBy.Alias')); } else if (fldName == 'CreatedByID') { options.add(new selectOption('STRING/' + fldPrefix + 'CreatedBy.Name', lblPrefix + 'CreatedBy.Name')); options.add(new selectOption('STRING/' + fldPrefix + 'CreatedBy.Alias', lblPrefix + 'CreatedBy.Alias')); } } return options; } public List<SelectOption> getLeadFields() { Map<String, Schema.SObjectField> leadFields = Schema.SObjectType.Lead.fields.getMap(); newScoreRuleController critClass = new newScoreRuleController(); //searchCriteria critClass = new searchCriteria(); // Return SelectOption lists for the Contact and Account objects List<SelectOption> sel1 = critClass.GetFieldsForObject(leadFields, '', ''); List<SelectOption> options = new List<SelectOption>(); options.add(new selectOption('', '- select field -')); for (Selectoption selOpt : sel1) { options.add(selOpt); } return options; } public List<SelectOption> getScoreRules() { List<SelectOption> options = new List<SelectOption>(); options.add(new selectOption('eq', 'Equals')); options.add(new selectOption('ne', 'Not Equal')); options.add(new selectOption('gt', 'Greater Than')); options.add(new selectOption('ge', 'Greater or Equal To')); options.add(new selectOption('lt', 'Less Than')); options.add(new selectOption('le', 'Less or Equal To')); options.add(new selectOption('starts', 'Starts With')); options.add(new selectOption('contains', 'Contains')); options.add(new selectOption('notcontain', 'Does Not Contain')); options.add(new selectOption('in', 'Includes')); options.add(new selectOption('notin', 'Excludes')); return options; } public List<SelectOption> getScoreVal() { List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('A','A')); options.add(new SelectOption('B','B')); options.add(new SelectOption('C','C')); return options; } public List<SelectOption> getWeightList() { List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('0','0')); options.add(new SelectOption('1','1')); options.add(new SelectOption('2','2')); options.add(new SelectOption('3','3')); options.add(new SelectOption('4','4')); options.add(new SelectOption('5','5')); options.add(new SelectOption('6','6')); options.add(new SelectOption('7','7')); options.add(new SelectOption('8','8')); options.add(new SelectOption('9','9')); return options; } public PageReference step1() { return Page.scoreRuleStep1; } public PageReference step2() { // Save some stuff here // Pass it through URL to the next page return Page.scoreRuleStep2; } public PageReference cancel() { PageReference leadsPage = new ApexPages.StandardController(lead).view(); leadsPage.setRedirect(true); return leadsPage; } public PageReference save() { // Create an object and save the rule and field there // TODO: Save an object NL__scoringRule__c myScoringRule = new NL__scoringRule__c(); PageReference leadPage = new ApexPages.StandardController(lead).view(); leadPage.setRedirect(true); return leadPage; } }
Here is my Visual Force Code:
<apex:page controller="newScoreRuleController" tabStyle="Lead"> <script> function confirmCancel() { var isCancel = confirm("Are you sure you wish to cancel?"); if (isCancel) return true; return false; } function leadChosen() { var isChosen = alert("Annual Revenue Chosen"); if (isChosen) return true; return false; } </script> <apex:sectionHeader title="Scoring Rule Wizard" subtitle="Step 2 of 2"/> <apex:form > <apex:pageBlock title="Choose a scoring rule" mode="edit"> <apex:pageBlockButtons > <apex:commandButton action="/apex/scoreRuleStep1" value="Previous"/> <apex:commandButton action="{!save}" value="Save"/> <apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Choose a scoring rule"> <apex:pageBlockTable value="{!ScoringRule}" var="sr" id="theTable" rowClasses="odd,even" styleClass="tableClass" rows="5"> <apex:column > <apex:facet name="header">Field</apex:facet> <apex:facet name="footer"></apex:facet> <apex:outputText value="{!fieldName}"/> </apex:column> <apex:column width="25%"> <apex:facet name="header">Rule 1</apex:facet> <apex:inputField value="{!sr.ScoringRule_Rule1__c}"/> </apex:column> <apex:column width="25%"> <apex:facet name="header">Value 1</apex:facet> <apex:inputText value="{!sr.ScoringRule_Val1__c}"/> </apex:column> <apex:column width="25%"> <apex:facet name="header">Rule 2</apex:facet> <apex:inputField value="{!sr.Rule2__c}"/> </apex:column> <apex:column width="25%"> <apex:facet name="header">Value 2</apex:facet> <apex:inputField value="{!sr.Val2__c}"/> </apex:column> </apex:pageBlockTable> <!-- Within a pageBlockSection, inputFields always display with their corresponding output label. --> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
I am very new to Apex code development.
I think the problem here are <apex
ageBlockTable> & <apex:inputField>
As a test - with slight modification I can see the data in a selection box:
public class newScoreRuleController {
Lead lead;
// public List<ScoringRule__c> criteriaLine = New List<ScoringRule__c>();
public List<ScoringRule__c> ScoringRule { get; private set;}
//public List<ScoringRule__c> ScoringRule;
public Lead getLead() {
if(lead == null) lead = new Lead();
return lead;
}
public String getFieldName() {
String s = ApexPages.currentPage().getParameters().get('fld');
return s;
}
public List<SelectOption> GetFieldsForObject(Map<String, Schema.SObjectField> objFields, string lblPrefix, string fldPrefix) {
// Build a list of field names to use to iterate the Map of field object pointers
Set <string> flds = new Set <String>();
flds = objFields.keySet();
// Add the keyset of field names to a list so that it can be sorted
List<String> fldList = new List<String>();
for (string f : flds) {
fldList.add(f);
}
fldList.sort();
List<SelectOption> options = new List<SelectOption>();
for (string f : fldList) {
string fldName = objFields.get(f).getDescribe().getName();
string fldLabel = objFields.get(f).getDescribe().getLabel();
string fldType = ('' + objFields.get(f).getDescribe().getType()).replace('Schema.DisplayType.', '') ;
if (fldType <> 'REFERENCE' && fldType <> 'ID' && fldName <> 'IsDeleted' && fldName <> 'SystemModstamp') options.add(new selectOption(fldType + '/' + fldPrefix + fldName, lblPrefix + fldLabel ));
if (fldName == 'OwnerID') {
options.add(new selectOption('STRING/' + fldPrefix + 'Owner.Name', lblPrefix + 'Owner.Name'));
options.add(new selectOption('STRING/' + fldPrefix + 'Owner.Alias', lblPrefix + 'Owner.Alias'));
options.add(new selectOption('STRING/' + fldPrefix + 'Owner.Department', lblPrefix + 'Owner.Department'));
options.add(new selectOption('STRING/' + fldPrefix + 'Owner.Division', lblPrefix + 'Owner.Division'));
} else if (fldName == 'LastModifiedByID') {
options.add(new selectOption('STRING/' + fldPrefix + 'LastModifiedBy.Name', lblPrefix + 'LastModifiedBy.Name'));
options.add(new selectOption('STRING/' + fldPrefix + 'LastModifiedBy.Alias', lblPrefix + 'LastModifiedBy.Alias'));
} else if (fldName == 'CreatedByID') {
options.add(new selectOption('STRING/' + fldPrefix + 'CreatedBy.Name', lblPrefix + 'CreatedBy.Name'));
options.add(new selectOption('STRING/' + fldPrefix + 'CreatedBy.Alias', lblPrefix + 'CreatedBy.Alias'));
}
}
return options;
}
public List<SelectOption> getLeadFields() {
Map<String, Schema.SObjectField> leadFields = Schema.SObjectType.Lead.fields.getMap();
newScoreRuleController critClass = new newScoreRuleController();
//searchCriteria critClass = new searchCriteria();
// Return SelectOption lists for the Contact and Account objects
List<SelectOption> sel1 = critClass.GetFieldsForObject(leadFields, '', '');
List<SelectOption> options = new List<SelectOption>();
options.add(new selectOption('', '- select field -'));
for (Selectoption selOpt : sel1) {
options.add(selOpt);
}
return options;
}
//****
public String ScoreRule
{
get;
set;
}
//****
public List<SelectOption> getScoreRules() {
List<SelectOption> options = new List<SelectOption>();
options.add(new selectOption('eq', 'Equals'));
options.add(new selectOption('ne', 'Not Equal'));
options.add(new selectOption('gt', 'Greater Than'));
options.add(new selectOption('ge', 'Greater or Equal To'));
options.add(new selectOption('lt', 'Less Than'));
options.add(new selectOption('le', 'Less or Equal To'));
options.add(new selectOption('starts', 'Starts With'));
options.add(new selectOption('contains', 'Contains'));
options.add(new selectOption('notcontain', 'Does Not Contain'));
options.add(new selectOption('in', 'Includes'));
options.add(new selectOption('notin', 'Excludes'));
return options;
}
public List<SelectOption> getScoreVal() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('A','A'));
options.add(new SelectOption('B','B'));
options.add(new SelectOption('C','C'));
return options;
}
public List<SelectOption> getWeightList() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('0','0'));
options.add(new SelectOption('1','1'));
options.add(new SelectOption('2','2'));
options.add(new SelectOption('3','3'));
options.add(new SelectOption('4','4'));
options.add(new SelectOption('5','5'));
options.add(new SelectOption('6','6'));
options.add(new SelectOption('7','7'));
options.add(new SelectOption('8','8'));
options.add(new SelectOption('9','9'));
return options;
}
public PageReference step1() {
return Page.scoreRuleStep1;
}
public PageReference step2() {
// Save some stuff here
// Pass it through URL to the next page
return Page.scoreRuleStep2;
}
public PageReference cancel() {
PageReference leadsPage = new ApexPages.StandardController(lead).view();
leadsPage.setRedirect(true);
return leadsPage;
}
public PageReference save() {
// Create an object and save the rule and field there
// TODO: Save an object
scoringRule__c myScoringRule = new scoringRule__c();
PageReference leadPage = new ApexPages.StandardController(lead).view();
leadPage.setRedirect(true);
return leadPage;
}
}
-----------------------------------------------------------------------------------------------------------------
<apex:page controller="newScoreRuleController" tabStyle="Lead">
<script>
function confirmCancel() {
var isCancel = confirm("Are you sure you wish to cancel?");
if (isCancel) return true;
return false;
}
function leadChosen() {
var isChosen = alert("Annual Revenue Chosen");
if (isChosen) return true;
return false;
}
</script>
<apex:sectionHeader title="Scoring Rule Wizard" subtitle="Step 2 of 2"/>
<apex:form >
<apex:pageBlock title="Choose a scoring rule" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="/apex/scoreRuleStep1" value="Previous"/>
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!cancel}" value="Cancel"
onclick="return confirmCancel()" immediate="true"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Choose a scoring rule">
<apex:pageBlockTable value="{!ScoringRule}" var="sr" id="theTable" rowClasses="odd,even"
styleClass="tableClass" rows="5">
<apex:column >
<apex:facet name="header">Field</apex:facet>
<apex:facet name="footer"></apex:facet>
<apex:outputText value="{!fieldName}"/>
</apex:column>
<apex:column width="25%">
<apex:facet name="header">Rule 1</apex:facet>
<apex:inputField value="{!sr.ScoringRule_Rule1__c}"/>
</apex:column>
<apex:column width="25%">
<apex:facet name="header">Value 1</apex:facet>
<apex:inputField value="{!sr.ScoringRule_Rule1__c}"/>
</apex:column>
<apex:column width="25%">
<apex:facet name="header">Rule 2</apex:facet>
<apex:inputField value="{!sr.Rule2__c}"/>
</apex:column>
<apex:column width="25%">
<apex:facet name="header">Value 2</apex:facet>
<apex:inputField value="{!sr.Val2__c}"/>
</apex:column>
</apex:pageBlockTable>
<!-- Within a pageBlockSection, inputFields always display with their
corresponding output label. -->
</apex:pageBlockSection>
</apex:pageBlock>
<apex:selectList size="1" id="dSource" value="{!ScoreRule}">
<apex:selectOptions value="{!ScoreRules}"/>
</apex:selectList>
</apex:form>
</apex:page>
--lvivanin