You need to sign in to do that
Don't have an account?
Reading value of inputField in apex controller.
Hi all,
I am trying to read inputField value from visualforce page to apex controller.
Below is the code for VF page, which allows user to choose the field from the picklist and will give its respectively.
<apex:page controller="dynamicInputField">
<apex:form id="Test2">
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="test" action="{!testfunc}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apeX:actionRegion >
<apex:selectList value="{!f}" multiselect="false" size="1" style="width:250px">
<apex:selectOptions value="{!items}"/>
<apex:actionSupport event="onchange" action="{!rensection}" reRender="Test2" />
</apex:selectList>
</apeX:actionRegion>
<apex:variable value="{!f}" var="test" />
<apex:inputField value="{!acc[test]}" rendered="{!rensec}" >
</apex:inputfield>
<!-- <apex:repeat value="{!field}" var="afield">
</apex:repeat> -->
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
controller :
public class dynamicInputField{
public dynamicInputField() {
acc = new account();
acclist = new list<account>();
rensec = false;
f= 'Industry';
field = new list<string>();
field.clear();
popFields();
system.debug('test my code cons'+acc);
}
public account acc{get;set;}
public list<string> field{get;set;}
public Map<String, Schema.SobjectField> fieldMap{get;set;}
public list<string> filterFields{get;set;}
public string f{get;set;}
public boolean rensec{get;set;}
public string tid{get;set;}
public string val{get;set;}
public list<account> acclist{get;set;}
public ApexPages.StandardController controller;
public pagereference testfunc(){
Schema.DescribeFieldResult r = Account.name.getDescribe();
Schema.sObjectField T = r.getSObjectField();
system.debug('testing my account '+ acc.name);
system.debug('test my account'+acc);
insert acc;
system.debug('test my account'+acc);
return null;
}
public void rensection(){
rensec = true;
system.debug('test my code'+f);
acclist.add(acc);
// field = new list<string>();
field.add(f);
system.debug('/test my code'+field);
system.debug('/test my code'+acc);
}
public dynamicInputField(ApexPages.StandardController controller){
this.controller = controller;
acc = new account();
rensec = false;
f= 'Industry';
field = new list<string>();
field.clear();
popFields();
}
public List<String> threeFieldList {
get {
if (threeFieldList == null) {
threeFieldList = new List<String>();
threeFieldList.add('Industry');
threeFieldList.add('AnnualRevenue');
threeFieldList.add('BillingCity');
}
return threeFieldList;
}
private set;
}
private void popFields() {
filterFields = new list<string>();
Schema.DescribeSObjectResult r = account.sObjectType.getDescribe();
system.debug('test my code fields :'+r);
fieldMap = Schema.SobjectType.account.fields.getMap();
for (String s : fieldMap.keySet()) {
system.debug('filterFieldstop'+s);
filterFields.add(s);
}
}
public void setvalue(){
acc = new Account();
String fvalue = ApexPages.CurrentPage().getParameters().get('fieldvalue');
system.debug('fvalue'+fvalue);
system.debug('fvalue21'+acc);
}
public List<SelectOption> items { get
{
List<String> sorted = new List<String>(filterFields);
sorted.sort();
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('none','none'));
for (String s : sorted ) {
system.debug('filterFields'+s);
options.add(new SelectOption(s,fieldMap.get(s).getDescribe().getLabel()));
}
return options;
}
}
}
----------------------------------------------------------------------------
I am not able to read the value of inputfield that user gives. Please help me how to read and store it in a variable, so that i can use it in a query.
Hi,
Try the below code as reference:
public pagereference testfunc()
{
Schema.DescribeFieldResult r = Account.name.getDescribe();
Schema.sObjectField T = r.getSObjectField();
system.debug('_______________testing my account '+ acc.name);
system.debug('___________ACCount______________'+acc.get(f)); // you can get value of input field in controller by //using acc.get(f)
acc.name=f;
system.debug('___________________test my account'+acc);
system.debug('__________testfunc______________________________________________test my code'+f);
insert acc;
system.debug('_________________test my account'+acc);
return null;
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
hi,
Thanks a lot for ur help.