Find your content:

Search form

You are here

How to write a dynamic code for this code


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( != null){
 Accname =;

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;

Attribution to: user1816707

Possible Suggestion/Solution #1

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

Attribution to: jordan.baucke

Possible Suggestion/Solution #2

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.

Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #3

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

Attribution to: techtrekker
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content