You need to sign in to do that
Don't have an account?
EagerToLearn
Iterate through an account list or any other object list and dynamically get the values of each field.
I am trying to dynamically iterate through an account list, for example but I do not want to hard code the field names.
Lets assume I performed a dynamic query before the code below to get just Name and Prospect fields.
This is what I don't want to do...
List<Account> accList = Database.query(queryString); //this line is fine but below is not dynamic...
for (Account a : accList) {
htmlBody += '<td>' + a.Name + '</td>';
htmlBody += '<td>' + a.Type + '</td></tr>';
}
...
This is what I want to do or something like it...
List<Account> accList = Database.query(queryString);
for (Account a : accList) {
for (fld f : fieldsInAccList) {
htmlBody += '<td>' + a.f + '</td>';
}
htmlBody += '</tr>'
}
Bottomline, I am trying to dynamically build an html table and I don't want to hard code anything if I can help it as that would require code changes everytime someone added a new field to the account object, for example. I am using a custom metadata table to hold a string of field names that I pass into a method that does everything dynamically but the above part!
Thanks for any help you can provide.
Lets assume I performed a dynamic query before the code below to get just Name and Prospect fields.
This is what I don't want to do...
List<Account> accList = Database.query(queryString); //this line is fine but below is not dynamic...
for (Account a : accList) {
htmlBody += '<td>' + a.Name + '</td>';
htmlBody += '<td>' + a.Type + '</td></tr>';
}
...
This is what I want to do or something like it...
List<Account> accList = Database.query(queryString);
for (Account a : accList) {
for (fld f : fieldsInAccList) {
htmlBody += '<td>' + a.f + '</td>';
}
htmlBody += '</tr>'
}
Bottomline, I am trying to dynamically build an html table and I don't want to hard code anything if I can help it as that would require code changes everytime someone added a new field to the account object, for example. I am using a custom metadata table to hold a string of field names that I pass into a method that does everything dynamically but the above part!
Thanks for any help you can provide.
WHERE the map contains the field api name as the key and the field label as the value.
List<Account> accList = Database.query(queryString);
String htmlBody = '<tr>';
for (Account a : accList) {
for (String fld : mapUsedtoBuildHtmlTable.keySet()) {
htmlBody += '<td>' + a.get(fld) + '</td>';
}
htmlBody += '</tr>';
}
htmlBody += '</table></body></html>';
String htmlTable = htmlHeader + htmlBody;