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

Getting field dynamically in Apex?
Hi all,
I have a query where i am getting the fieldname dynamically from a peice of code. Then i have a loop and i want to access that field's value. Its not letting me do that.
Here is my code -
String leadquery = 'select ' + fldName + ' from lead';
List <Lead> leads = Database.Query(leadquery);
for (Lead lead : leads) {
Integer val_fld = Integer.valueOf(lead.fldName);
// Do something
}
It doesnt let me do lead.fldName bcause it says "Invalid field fldName for sObject lead." Can anyone tell me how can i do it dynamically?
Any help is much appreciated.
Change the name of your field string,If you have a field with the name of "fldName" in lead.
Try this,
String leadquery = 'select ' + fieldsToSelect + ' from lead';
List <Lead> leads = Database.Query(leadquery);
for (Lead lead : leads) {
Integer val_fld = Integer.valueOf(lead.fldName);
// Do something
}
But it looks like that you don't have a field "fld.Name" in lead.
So try this,
String leadquery = 'select ' + fieldsToSelect + ' from lead';
List <Lead> leads = Database.Query(leadquery);
for (Lead lead : leads) {
String val_fld = lead.Name;
// Do something
}
If it works fine, then problem is not with selection,You don't have field with "fld.Name" in lead