You need to sign in to do that
Don't have an account?
Karthik Mano
Dynamically getting field values from Object
Hi,
I have a requirement where the User dynamically selects a field from the dropdown(VF select Option) and i have to check whether those field values are available in a map or not.
I have a Select Option in Vf where all the "Account" fields will be available. The selected field's API name will be set in a get set "selectedField"
public String selectedField{get;set;} // This is the String where the API name of selected field will get set
and here is my code,
List<Account> accList = new List<Account>([SELECT Id,NumberofLocations__c FROM Account WHERE NumberofLocations__c != NULL]);
Map<String,Account> accMap = new Map<String,Account>();
for(Account acc:accList) {
accMap.get(acc.selectedField); // Here is the problem, I need to get the value from the map based on the field selected by the user. I get an error selectedField does not exist in Account object.
}
I tried an alternative,
for(Account acc:accList) {
String val = 'acc.'+selectedField;
accMap.get(val); // No success with this approach too.
}
Suggestions please ??
I have a requirement where the User dynamically selects a field from the dropdown(VF select Option) and i have to check whether those field values are available in a map or not.
I have a Select Option in Vf where all the "Account" fields will be available. The selected field's API name will be set in a get set "selectedField"
public String selectedField{get;set;} // This is the String where the API name of selected field will get set
and here is my code,
List<Account> accList = new List<Account>([SELECT Id,NumberofLocations__c FROM Account WHERE NumberofLocations__c != NULL]);
Map<String,Account> accMap = new Map<String,Account>();
for(Account acc:accList) {
accMap.get(acc.selectedField); // Here is the problem, I need to get the value from the map based on the field selected by the user. I get an error selectedField does not exist in Account object.
}
I tried an alternative,
for(Account acc:accList) {
String val = 'acc.'+selectedField;
accMap.get(val); // No success with this approach too.
}
Suggestions please ??
Now I got the issue :)
hey can you try to do the below,
for(Account acc:accList) {
String fieldValue = (String) acc.get(selectedField);
}
All Answers
I am not clear here but this may help you,
Are you using API names (ex: Test__c) in VF page selection?
I dont see any data in accMap here, do you want to query all account details into Map and then get value from map based on VF page selection ?
If you are using this VF page on account then you can use the below code,
String accId = ApexPages.currentPage().getParameters().get('Id');
Map<Id, Account> accmap = new map<Id, Account>([select Id, NumberofLocations__c, all VF page fields(selectedField) from Account where NumberofLocations__c != NULL] ]);
String accFieldValue;
for(Account acc: accmap.values()){
accFieldValue = accmap.get(accId).selectedField;
}
The basic idea is i am constructing the accMap here based on user selected field (Select Option). This is how it goes..
public String selectedField{get;set;} // The field selected by the user
List<Account> accList = new List<Account>();
accList = Database.Query('SELECT '+selectedField+' FROM Account');
Map<String,List<Account>> accMap = new Map<String,Account>();
for(Account acc:accList) {
// If the value is not already present in the map
if(accMap.get(acc.selectedField) == NULL) { // I get an error here as "selectedField" does not exist in Account Object. This "selectedField" is a field which i get dynamically from the User input
List<Account> tempList = new List<Account>();
(accMap.get(acc.selectedField)).put(tempList); // Error
}
// If the value is already present in the map
else {
(accMap.get(acc.selectedField)).add(acc); // Error
}
}
Hope you got the issue now ???
Now I got the issue :)
hey can you try to do the below,
for(Account acc:accList) {
String fieldValue = (String) acc.get(selectedField);
}