1

I want to Display in roll up data in table of visualforce page. Scenarios:

  1. Table contain list of account types and every account have multiple fields.
  2. want to rollup every account types fields and display in table.

for examaple:- like their is 3 eduction type account and every account have some balance, high credit, paydue etc. i want to rollup their balance, high credit, paydue and display in table.

Example of table what i want

in this table education type account is 3 and every account have some balance like 100, 50 150 and their rollup is 300 which is display in table like that every row cell are rollup of fields.

I hope m well defined the Scenarios.

to full fill that senarios i approach in that way:

this is my controller class code:

public AggregateResult[] result;

result = [SELECT Credit_Loan_Type__c type,COUNT(id) num,SUM(Account_Balance__c) totalBalance,SUM(High_Credit_Amount__c) highCredit,
                      SUM(Monthly_Payment__c) monthlyPayment,SUM(Past_Due_Amount__c) dueAmount FROM Trade_Accounts__c 
                      Where Personal_Credit_Report__r.Contact__c =:conId
                      GROUP BY Credit_Loan_Type__c ];

 Map<String,String> typs = new Map<String,String>();
        typs.put('MORTGAGE', 'MORTGAGE');
        typs.put('AUTO', 'AUTO');
        typs.put('EDUCATION', 'EDUCATION');
        typs.put('INSTALLMENT', 'OTHER INSTALLMENT');
        typs.put('OPEN', 'OPEN');
        typs.put('CreditCard', 'REVOLVING');
        typs.put('OTHER', 'OTHER');



        reportList= new List<WrappperReport>();
        for(AggregateResult agr : result){
            System.debug('agr+++=='+agr);
        for(string ids : typs.keySet()){
           System.debug('idss+=='+ids);
               WrappperReport  wrp = new WrappperReport();
                if(agr.get('type') == ids ){ 
                    wrp.typeo = typs.get(ids);
                    wrp.count = Integer.valueOf((Decimal)agr.get('num'));
                   wrp.balance = Integer.valueOf((Decimal)agr.get('totalBalance'));
                   wrp.highCredit = Integer.valueOf((Decimal)agr.get('highCredit'));
                   wrp.payment = Integer.valueOf((Decimal)agr.get('monthlyPayment'));
                   wrp.pastDue = Integer.valueOf((Decimal)agr.get('dueAmount'));
                   reportList.add(wrp);                   
                }else{
                    wrp.typeo = typs.get(ids);
                    wrp.count = 0;
                    wrp.balance = 0;
                    wrp.highCredit = 0;
                    wrp.payment = 0;
                    wrp.pastDue = 0;
                    reportList.add(wrp);
                }

public class WrappperReport{
        public string typeo {get;set;}
        public Integer count {get;set;}
        public Integer balance {get;set;}
        public Integer highCredit {get;set;}
        public Integer payment {get;set;}
        public Integer pastDue {get;set;}

    }

and this is my vf page code to display that table:

 <tbody>

                <apex:pageBlock >

        <apex:repeat value="{!reportList}" var="item">
            <tr>
                <td>{!item.typeo}</td>
                <td>{!item.count}</td>
                <td>{!item.balance}</td>
                <td>{!item.highCredit}</td>
                <td>{!item.payment}</td>                
                <td>{!item.pastDue}</td>              
            </tr>
        </apex:repeat>
   </apex:pageBlock>  
            </tbody>

its working fine for single type of account but if added different types of account then table create multiple times for every types

output of my code

1 Answer 1

0

It's your if-else part that's causing the problem. Essentially, you're adding each type for every queried type, causing a multiplicative effect. Instead, consider using a Map instead. What we'll do here is initialize all values to 0, then populate them from the result set. This means that you won't get the multiplicative effect you're seeing here.


Map<String, WrapperReport> output = new Map<String, WrapperReport>();
for(String ids: typs.keySet()) {
  WrapperReport wrapper = new WrapperReport();
  wrapper.typeo = typs.get(ids);
  wrapper.count = 0;
  wrapper.balance = 0;
  wrapper.highCredit = 0;
  wrapper.payment = 0;
  wrapper.pastdue = 0;
  output.put(ids, wrapper);
}
for(AggregateResult agr : result){
   WrapperReport wrp = output.get((String)agr.get('type'));
   wrp.count = Integer.valueOf((Decimal)agr.get('num'));
   wrp.balance = Integer.valueOf((Decimal)agr.get('totalBalance'));
   wrp.highCredit = Integer.valueOf((Decimal)agr.get('highCredit'));
   wrp.payment = Integer.valueOf((Decimal)agr.get('monthlyPayment'));
   wrp.pastDue = Integer.valueOf((Decimal)agr.get('dueAmount'));
}
reportList = output.values();
5
  • thanxx for reply... But When i tried like this way then i always getting Attempt to de-reference a null object error. I tried this way but still getting same error
    – Hennry
    Sep 5, 2018 at 7:13
  • if((Decimal)agr.get('num') !=Null) wrp.count = Integer.valueOf((Decimal)agr.get('num')); else wrp.count = 0; if((Decimal)agr.get('totalBalance') >0) wrp.balance = Integer.valueOf((Decimal)agr.get('totalBalance')); else wrp.balance = 0; if((Decimal)agr.get('highCredit') >0) wrp.highCredit = Integer.valueOf((Decimal)agr.get('highCredit')); else wrp.highCredit = 0;
    – Hennry
    Sep 5, 2018 at 7:14
  • @sanukumarverma It's a case-sensitivity issue then. Make sure the case for each value matches a valid Credit Loan Type value.
    – sfdcfox
    Sep 5, 2018 at 7:15
  • @sanukumarverma That's all unnecessary code. And probably wrong code anyways; if totalBalance is null, you will get a NullPointerException checking totalBalance > 0.
    – sfdcfox
    Sep 5, 2018 at 7:17
  • thnxx now its working...thnxx for help
    – Hennry
    Sep 5, 2018 at 7:25

You must log in to answer this question.

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