function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MukulMukul 

Cant display custom object fields.

Hi all,

 

I have this issue where i cant display my custom object;s fields on the page. Can anyone help me out?

 

Here is my Controller Code:

 

public class newScoreRuleController {

public List<ScoringRule__c> ScoringRule { get; private set;}

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() {
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;
}
</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. 

Best Answer chosen by Admin (Salesforce Developers) 
Ron HessRon Hess

New objects are created with new 

 

so you need to construct these so that the page can render them.

 

here is a constructor that builds a list of 3 empty records.

 

 

public newScoreRuleController () {

 

ScoringRule = new List<ScoringRule__c>();

ScoringRule.add( new ScoringRule__c() );

ScoringRule.add( new ScoringRule__c() );

ScoringRule.add( new ScoringRule__c() );

 

}

 

 

as you see, first you need a list so that the memory is valid, then you add empty records to the list. 

 

 

Best recommendation is to get a sample working with standard objects, use the wizard example so that you understand how records are created and inserted into the database.

 

here http://www.salesforce.com/us/developer/docs/pages/Content/pages_quick_start_wizard.htm 

 

Message Edited by Ron Hess on 06-16-2009 01:41 PM

All Answers

David VPDavid VP

Did you check your object CRUD permissions on the profile and the field level security ?

If your profile isn't allowed to see the fields then they won't show up in the VisualForce page.

 

David

Ron HessRon Hess

Can you add some System.Debug() statements to you code so that you can see if the list is empty or not.

 

from looking over your code, this variable

 

public List<ScoringRule__c> ScoringRule { get; private set;} 

 

Is declared but never set, so it will always be empty.

 

did you intend to assign some list to this property ?

 

normally you would have a constructor that fills this in.

 

sort of like this : 

 

 

public newScoreRuleController () { ScoringRule = [ select id,name , blah... from ScoringRule__c where some_condition_that_you_want = true limit 10]; }

 

 

 otherwise that list is always empty when the page tries to draw this : 

 

<apex:pageBlockTable value="{!ScoringRule}" 

 

 

MukulMukul

Thanks Ron. I got your point. My object fields are currently empty. I just want to display the text fieldsand select boxes so that the user can enter the values.

 

Can i do that?

 

Thanks again.
Ron HessRon Hess

New objects are created with new 

 

so you need to construct these so that the page can render them.

 

here is a constructor that builds a list of 3 empty records.

 

 

public newScoreRuleController () {

 

ScoringRule = new List<ScoringRule__c>();

ScoringRule.add( new ScoringRule__c() );

ScoringRule.add( new ScoringRule__c() );

ScoringRule.add( new ScoringRule__c() );

 

}

 

 

as you see, first you need a list so that the memory is valid, then you add empty records to the list. 

 

 

Best recommendation is to get a sample working with standard objects, use the wizard example so that you understand how records are created and inserted into the database.

 

here http://www.salesforce.com/us/developer/docs/pages/Content/pages_quick_start_wizard.htm 

 

Message Edited by Ron Hess on 06-16-2009 01:41 PM
This was selected as the best answer
MukulMukul

Thanks Ron. I will go through the link. I appreciate all your help. This has been a tremendous help.

 

Regards

hgolovhgolov

This was exactly what I needed - the field level permissions. Thank you very much.