按季度的帐户摘要

时间:2015-08-20 17:55:21

标签: salesforce

我试图获得一个帐户列表,其中每个qtr / year的累计机会总数。例如,它可能看起来像

Account    2015 Q1       2015 Q2        2015 Q3
AccountA    50000         25000          75000
AccountB    22000         66000          28000

而且我知道如何使用汇总结果来获得季度和财政年度,如何使季度和年份按照描述水平,而不是以数据库格式垂直。

aggOppList = [SELECT account.Name
                   , sum(amount) TotalAmount
                   , FISCAL_QUARTER(closedate) qtr
                   , FISCAL_Year(closedate) year 
              FROM Opportunity
              WHERE accountID IN :acctList
              GROUP By account.name
                   , FISCAL_QUARTER(closedate)
                   , FISCAL_YEAR(closedate)];

感谢。

1 个答案:

答案 0 :(得分:0)

我无法找到一个简单的解决方案,但下面就是我所拥有的。

public class FiscalPivot_Class {


List<AggregateResult> aggOpp;

public List<string> periodYearList {get; set; }
PivotTimeSeries_Class.PivotRowAndTimeWrapper completeRowWrapper;  //    takes  (String name, string accountId, map<string, decimal> amountMap, decimal lineTotal)
public List<PivotTimeSeries_Class.PivotRowWrapper> completeRowList {get; set; }


public FiscalPivot_Class() {
    aggOpp  = [SELECT sum(amount) TotalAmount, accountId, account.name varName, CALENDAR_QUARTER(closedate) period, CALENDAR_YEAR(closedate) year FROM Opportunity 
                   GROUP BY CALENDAR_QUARTER(closedate), CALENDAR_YEAR(closedate), account.name, accountId ORDER BY account.name ASC, CALENDAR_YEAR(closedate) ASC, CALENDAR_QUARTER(closedate) ASC ];
    system.debug('aggOpp= ' + aggOpp);
    completeRowWrapper = PivotTimeSeries_Class.pivotTimeSeries(aggOpp);
    completeRowList = completeRowWrapper.Wrapper;
    periodYearList = completeRowWrapper.periodYearList;

    system.debug('completeRowList=' +completeRowList);



}  // end the constructor


} // end the class

public class PivotTimeSeries_Class {
/*This class takes a aggregate result and returns a wrapper that give a horitzontal perspective to time series data
 * The time series should have the following key variables
 * totalAmount  - sum of amounts
 * period  - should be aggregated close date either month or quarter
 * year - aggregated years
 * aName - the variable to have vertically
 * 
 * 
 * */

    public  static PivotRowAndTimeWrapper pivotTimeSeries(List<AggregateResult> aggOpp) {
       // part 1 take the aggregate result and turn it into a list
        set<string> periodYearSet = new set<string>(); // the complete list of periods and years in string form
        List<string> periodYearList = new List<string>();
        List<AggListWrapper> wrapperList = new List<AggListWrapper>();
        List<PivotRowWrapper> completeRowList = new List<PivotRowWrapper>();
        try {
            for (AggregateResult agg:aggOpp) {
                String name = (string)agg.get('varName');
                String acctId = (string)agg.get('accountID');
                Integer period = (Integer)agg.get('period');
                Integer year = (Integer)agg.get('year');
                Decimal amount = (Decimal)agg.get('totalAmount');
                if (amount == NULL ) { amount = 0;}
                AggListWrapper wrapper = new AggListWrapper(name, acctId, amount, period, year);
                wrapperList.add(wrapper);
               }

        } catch (exception e) { 
            system.debug('error in the aggOpp loop ' + e);
        }
        system.debug('end of part 1 wrapperList = ' + wrapperList);
        // end part 1 we now have a full wrapper list

//****************************************************************************        
        // part 2 
        Map<string, Decimal> amountMap = new Map<String, Decimal>();
        string lastname;
        string name;
        string accountId;
        try {
            for (AggListWrapper wrapper:wrapperList) {
                name = wrapper.name;
                accountId = wrapper.accountId;
                decimal amount = wrapper.amount;
                string periodYear = ''+ wrapper.year + '-' + wrapper.period;
                periodYearSet.add(periodYear);
                if (lastName == NULL) { lastName=name;}
                if (lastName != name) {
                    completeRowList.add(new PivotRowWrapper(lastName, accountId, amountMap, NULL));
                    amountMap = new Map<string, Decimal>();
                 }
                amountMap.put(periodYear, amount);
                system.debug('**** end of loop name=' + name + ' lastName=' + lastName + ' amountMap=' + amountMap);
                lastName = name;
             }
        } catch (exception e) {
            system.debug('error in the wraperList lopp ' + e);
        }
        periodYearList.addAll(periodYearSet);
        periodYearList.sort();
         completeRowList.add(new PivotRowWrapper(name, accountId, amountMap, NULL));  // line total is null when we first go through        
        system.debug('end part 2 - complteRowList = ' + completeRowList);
        // end part 2
//****************************************************************************         

    // Part 3   - take map of account names (sub map of time periods) and loop through the time periods and add zero's to any missing time periods         
        List<PivotRowWrapper> updatedRowList = new List<PivotRowWrapper>();
        for (PivotRowWrapper wrapper: completeRowList) {
            name = wrapper.name;
            accountId = wrapper.accountId;
            amountMap = new Map<string, Decimal>();
            Decimal lineTotal= 0;
            Decimal amount = 0;
            for (String qtrYear :periodYearList) {
                if(wrapper.amountMap.containsKey(qtrYear)) {
                    amount = wrapper.amountMap.get(qtrYear);
                    amountMap.put(qtrYear, amount); 
                    lineTotal += amount;
                } else {
                    amountMap.put(qtrYear, 0);   
                }
            }
            updatedRowList.add(new PivotRowWrapper(name, accountId, amountMap, lineTotal));
        }
        completeRowList = updatedRowList;
        system.debug('Part 3 completeRowList AFTER = ' + completeRowList);
        system.debug('period list ' + periodYearList);
        PivotRowAndTimeWrapper completeRowTimeWrapper = new PivotRowAndTimeWrapper(completeRowList, periodYearList);

        return completeRowTimeWrapper;

}  // end the static pivot time series method





// *****************************************************************************

        public class QtrSalesWrapper {
        // takes data from an List and allows for the combining of the amounts according to period/year into a map
        public string name;
        public string accountId;
        public Map<String, Decimal> qtrSalesAmount;

        public QtrSalesWrapper(String name, string accountId, Map<String, Decimal> qtrSalesAmount) {
            this.name = name;
            this.accountId = accountId;
            this.qtrSalesAmount = qtrSalesAmount;
         }
    }  // end QtrSalesWrapper

    public class AggListWrapper {
        // takes data from an aggregate result query and turns it into a list with the appropriate variables   
        public string name;
        public string accountId;
        public Decimal amount;
        public Integer period;
        public Integer year;

        public AggListWrapper(String name, string accountId, Decimal amount, Integer period, Integer year) {
            this.name = name;
            this.accountId = accountId;
            this.amount = amount;
            this.period = period;
            this.year = year;
        }
    } // end the aggListWrapper


     public class PivotRowWrapper {
        public string name {get; set; }
        public string accountId {get; set;}
        public map<string, decimal> amountMap {get; set;}
        public decimal lineTotal {Get; set; }

        public PivotRowWrapper(String name, string accountId, map<string, decimal> amountMap, decimal lineTotal) {
            this.name = name;
            this.accountId = accountId;
            this.amountMap = amountMap;
            this.lineTotal = lineTotal;
        }        
      }  // end the wrapper class


       public class PivotRowAndTimeWrapper {
           public List<PivotRowWrapper> wrapper {get; set; }
           public List<String> periodYearList {get; set; }

           public PivotRowAndTimeWrapper(List<PivotRowWrapper> wrapper, List<String> periodYearList) {
               this.wrapper = wrapper;
               this.periodYearList = periodYearList;
           }
      }





} // end the main clasS*****************

<apex:page controller="FiscalPivot_Class">


<apex:pageblock >
<apex:pageblocktable value="{!completeRowList}" var="row">
     <apex:column >
      <apex:outputtext >{!row.name}</apex:outputtext>
    </apex:column>


    <apex:repeat value="{!periodYearList}" var="periodYear">
   <apex:column headerValue="{!periodYear}" >

        <apex:outputtext value="{0,number,###,###,##0}" ><apex:param value="{!row.amountMap[periodYear]}"/></apex:outputtext> 
      </apex:column>
    </apex:repeat>

     <apex:column >
    <apex:facet: name="header">Total</apex:facet:>           
    <apex:outputtext value="{0,number,###,###,##0}" ><apex:param value="{!row.lineTotal}"/></apex:outputtext> 
    </apex:column>

    </apex:pageblocktable>
</apex:pageblock>