Hi I designed this vf Page to display records from the bank object with total at the bottom of the Page actually there are two number fields In bank object October,November. In future if other months are added In object then how to write the dynamic code for future purpose with out modifying the code again.


 public with sharing class Class{

 public String Accname {get; set;}

public Class(ApexPages.StandardController controller) {
if(apexpages.currentpage().getparameters().get('id') != null){
Account objA = [select id,name from Account where id =:   apexpages.currentpage().getparameters().get('id')];

if(objA.name != null){
 Accname = objA.name;

double octoberSum =0;
double novemberSum =0;

 lstWT = new List<wrapperTurn>();

 for(bank__c objTW : [select ICS_Product_Name__c,October__c,November__c from bank__c   where account__c=: apexpages.currentpage().getparameters().get('id') order by   createddate desc limit 10]){
objwt = new wrapperTurn(objTW);

 if(objTW.october__c!= null)
 octoberSum = octoberSum + objTW.october__c;
 if(objTW.november__c != null)
  novemberSum = novemberSum + objTW.november__c;

 bank__c objTWNull = new bank__c();
 lstWT.add(new wrapperTurn(objTWNull));
 bank__c objTW = new bank__c(ICS_Product_Name__c =  'TOTAL VALUE',October__c =   octoberSum,November__c = novemberSum);
 lstWT.add(new wrapperTurn(objTW));
  public List<bank__c> turnaways{get;set;}
  public wrapperTurn objwt{get;set;}
  public List<wrapperTurn> lstWT{get;set;}

  public Class(){


 public class wrapperTurn{
 public bank__c objTW{get;set;}
 public wrapperTurn(bank__c objTW){
 this.objTW = objTW;
  • @jordan.baucke. i understand the code but what i want to know is how to do the double method operation and totalling the number field at the bottom of the page
    – user904
    Commented Nov 13, 2012 at 11:06

3 Answers 3


You will want to use dynamic apex to query all the available fields at any given time from the sObject like so:

(This method takes an object name, as well as some relationship, and where clauses and returns a list of sObjects, which you can than cast to your type, in this case bank__c

 *  List<sObject> queryAllSObjectFields()
 *  Takes a name of an sObject, and any relationship fields necessary
    global List<sObject> queryAllSObjectFields(String objectName, Map<String,String> whereClauses, Map<string,string> relationships){
        // Initialize setup variables
        String query = 'SELECT';
        Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap();

        Integer i = 0;
        // Grab the fields from the describe method and append them to the queryString one by one.
        for(String s : objectFields.keySet()) {
            if(i < objectFields.keySet().size() - 1){
                query += ' ' + s + ', ';
                query += ' ' + s;

        // add relationships Map<ObjectName,RelationshipName> relationships
        // need to update to add more relationships
        if(relationships != null)
            for(String r : relationships.keySet()) {
                String relationshipQuery = ', (SELECT ';
                Map<String, Schema.SObjectField> relationshipFields = Schema.getGlobalDescribe().get(r).getDescribe().fields.getMap();
                Integer j = 0;
                for(String rField : relationshipFields.keyset()){
                    if(j < relationshipFields.keySet().size() - 1){
                        relationshipQuery += ' ' + rField + ', ';
                        relationshipQuery += ' ' + rField;
                relationshipQuery += ' FROM '+ relationships.get(r) + ') '; 
                query += relationshipQuery; 

        // Add FROM statement
        query += ' FROM ' + objectName;

        // Add on a WHERE/ORDER/LIMIT statement as needed
        if(whereClauses.size() > 0){    
            for(String key : whereClauses.keyset()){    
                query += ' WHERE '+ key +' = \''+ whereClauses.get(key) + '\''; // modify as needed

        query += ' ORDER BY Id';

        try {
            List<sObject> sObjects = database.query(query);
            return sObjects;
        } catch (QueryException e){
            system.debug('Query All sObject Fields Exception: '+e);
            return null;    //perform exception handling
  • 1
    One thing to beware of when pulling all fields this way - its quite easy to get into a situation where you are blowing your heap space due to the number and size of the records retrieved. Especially if you have large/rich text area fields. Commented Nov 13, 2012 at 12:10
  • Hmmm I hadn't thought about that - I typically return the results of these queries versus @RemoteAction would it still be an issue than in runtime or just if I was directly referencing all the fields in the view? Commented Nov 13, 2012 at 17:08
  • @RemoteAction would still potentially suffer from the problem as the heap space would be subject to the governor limit for the request. It wouldn't have a viewstate problem, unlike regular VF and forms. Commented Nov 13, 2012 at 17:41
  • @BobBuzzard I see...so I caveat my answer with that -- 6MB governor limit (I believe is heap size concern here), so this is probably unsafe for Content type encoded fields! Commented Nov 13, 2012 at 18:42

You can use custom settings for the same.Have a custom settings that store same fields as you have in your actual objects.

Also please document for the admin that he dont forget to add these fields to custom settings once he makes an addition to Object to reflect in code .

Coming to modifications that you need in code

1)Genearte dynamic query with values including from custom settings 2)Use Schema calls to get fields from its token stored in the custom settings .

an example as below:

Map<String, Schema.SObjectField> M = Schema.SObjectType.Account.fields.getMap();

3)To assign values you may use dynamic DML

an example


In short custom settings ,schema describe calls and dynamic DML can be used to design this code for dynamic addition.


I'm not sure if there should be dedicated fields for each month or just one field (maybe Picklist) which is Month__c (either Jan - Dec or 1-12) and then a field for Amount (Unless of course both Oct and Nov can have a not null value on the same record)

You can then dynamically select bank based on the totals you need to show, without having to explicitly select Oct, Nov

You can then probably use an aggregate query to select the total per month for the selected account

Select Month__c, Sum(Amount__c) from     Bank__c where account__c = ..... Group By Month__c

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .