---
schema_version: "1"
id: 3a1b4511d3f2e7a513145519a957c6fe644e81972d87cd3202e4c5921085f4eb
title: TPM_RTRFixedFunds_AMS
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/cgcloud_rtr_dev_guide/tpm_rtr_class_tpm_rtrfixedfunds_ams.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.cgcloud_rtr_dev_guide.meta/cgcloud_rtr_dev_guide/tpm_rtr_class_tpm_rtrfixedfunds_ams.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: f03ee61e41d669a8fde553f94319d9b3346260774bc1b44d7d7c93799d4591a8
description: Sample of the TPM_RTRFixedFunds_AMS class.
---
# TPM\_RTRFixedFunds\_AMS

Sample of the TPM\_RTRFixedFunds\_AMS class.

global class TPM\_RTRFixedFunds\_AMS{
global static List<TPM\_RTRReportingWrapper\_AMS.OutputRecord> getBrand(Date inputDateBegin, Date inputDateEnd, TPM\_RTRReportingWrapper\_AMS.InputPayload payload, List<TPM\_RTRReportingWrapper\_AMS.OutputRecord> output, Map<String, TPM\_RTRRouting\_\_mdt> fixedFunds){
//This code creates a list based on the map that was passed into the method. In this List only the Fund Template
//Names are stored so that the list can be used in the AggregateResult query (where clause). The goal is to only load records that
//are related to the Fund Templates we're looking at.
List<String> FundTemplateTypes = New List<String>();
String SalesOrg;
For(String cmID : fixedFunds.keyset()){
FundTemplateTypes.add(fixedFunds.get(cmID).Fund\_Template\_\_c);
if(Salesorg == Null){
Salesorg = fixedFunds.get(cmID).Sales\_Org\_\_c;
}
}
//The query returns Fund\_Transaction\_Row records based on the selected timeframe, customer, category and templates
//The results are aggregated TPM\_RTR\_Amount values grouped by BRAND, FUND TEMPLATE
AggregateResult\[\] groupedResults = \[SELECT SUM(TPM\_RTRAmount\_\_c) amount,
cgcloud\_\_Target\_Fund\_\_r.cgcloud\_\_Fund\_Template\_\_r.Name template,
cgcloud\_\_Target\_Fund\_\_r.cgcloud\_\_Fund\_Template\_\_r.TPM\_RTRRoutingFundType\_\_c RTRRoutingFundType,
cgcloud\_\_Fund\_Transaction\_\_r.cgcloud\_\_Fund\_Transaction\_Header\_\_r.TPM\_Product\_\_c brand
FROM cgcloud\_\_Fund\_Transaction\_Row\_\_c
WHERE cgcloud\_\_Target\_Fund\_\_r.cgcloud\_\_Fund\_Template\_\_r.TPM\_RTRRoutingFundType\_\_c in :FundTemplateTypes
AND cgcloud\_\_Target\_Fund\_\_r.cgcloud\_\_Sales\_Org\_\_c= :SalesOrg
AND cgcloud\_\_Fund\_Transaction\_\_r.cgcloud\_\_Fund\_Transaction\_Header\_\_r.TPM\_Product\_\_r.cgcloud\_\_Criterion\_1\_Product\_\_c IN :payload.productsfids
AND cgcloud\_\_Target\_Fund\_\_r.cgcloud\_\_Anchor\_Account\_\_c IN :payload.accountsfids
AND cgcloud\_\_Transaction\_Type\_\_c IN ('Deposit','Withdraw')
AND ((cgcloud\_\_Target\_Fund\_\_r.cgcloud\_\_Valid\_From\_\_c <= :inputDateEnd
AND cgcloud\_\_Target\_Fund\_\_r.cgcloud\_\_Valid\_From\_\_c >= :inputDateBegin) OR (cgcloud\_\_Target\_Fund\_\_r.cgcloud\_\_Valid\_Thru\_\_c <= :inputDateEnd
AND cgcloud\_\_Target\_Fund\_\_r.cgcloud\_\_Valid\_Thru\_\_c >= :inputDateBegin))
GROUP BY cgcloud\_\_Fund\_Transaction\_\_r.cgcloud\_\_Fund\_Transaction\_Header\_\_r.TPM\_Product\_\_c, cgcloud\_\_Target\_Fund\_\_r.cgcloud\_\_Fund\_Template\_\_r.Name, cgcloud\_\_Target\_Fund\_\_r.cgcloud\_\_Fund\_Template\_\_r.TPM\_RTRRoutingFundType\_\_c\];
String brand, template, RTRRoutingFundType;
Double amount;
Map<String, Decimal> templateMap;
Map<String, Map<String, Decimal>> cmap = New Map<String, Map<String, Decimal>>();
Map<String, String> RTRRoutingFundTypeMap = New Map<String, String>();
//This For-Loop works on the aggregateResults and allocates all the records to their respective "place" -
//it basically sorts all data into the right maps
For(AggregateResult ar : groupedResults){
brand = String.valueOf(ar.get('brand')); //the last part of this line retrieved the brand id of our query (brand is an alias)
template = String.valueOf(ar.get('template')); //the last part of this line retrieved the template Name of our query (template is an alias)
RTRRoutingFundType = String.valueOf(ar.get('RTRRoutingFundType')); //the last part of this line retrieved the RTR Routing Fund Type of our query (RTRRoutingFundType is an alias)'));
amount = Double.valueOf(ar.get('amount')); //the last part of this line retrieved the aggregated amount of our query (amount is an alias)
templateMap = New Map<String, Decimal>();
If(!cmap.containsKey(brand)){
templateMap.put(template, amount);
cmap.put(brand, templateMap);
}
else {
templateMap = cmap.get(brand);
if(!templateMap.containsKey(template)){
templateMap.put(template, amount);
cmap.put(brand, templateMap);
}
}
if(!RTRRoutingFundTypeMap.containskey(RTRRoutingFundType)){
RTRRoutingFundTypeMap.put(RTRRoutingFundType, template);
}
}
//Once all the results of our query are allocated and sorted, the data needs to be written back to the report
//For each brand in cmap,
For(String brandId : cmap.keyset()){
//and each template for that brand,
For(String templateId : cmap.get(brandId).keyset()){
//Now we loop trough the map that was passed to this method
For(String ffID : fixedFunds.keyset()){
//to check to which KPI the map we're currently in is corrosponding to
If(templateId == RTRRoutingFundTypeMap.get(fixedFunds.get(ffID).Fund\_Template\_\_c)){
//If the map we're working on right now has the RDF Fund Template, and one of the records in our map has
//the RDF Fund Template assigned, we write back this KPI to the corrosponding KPI Definition of the Custom Meta Data
output.add(new TPM\_RTRReportingWrapper\_AMS.OutputRecord(String.valueOf(brandId), String.valueOf(fixedFunds.get(ffID).KPI\_Definition\_\_c), 'Total', Double.valueOf(cmap.get(brandId).get(templateId))));
}
}
}
}
return output;
}
global static List<TPM\_RTRReportingWrapper\_AMS.OutputRecord> getCategory(Date inputDateBegin, Date inputDateEnd, TPM\_RTRReportingWrapper\_AMS.InputPayload payload, List<TPM\_RTRReportingWrapper\_AMS.OutputRecord> output, Map<String, TPM\_RTRRouting\_\_mdt> fixedFunds){
//This code creates a list based on the map that was passed into the method. In this List only the Fund Template
//Names are stored so that the list can be used in the AggregateResult query. The goal is to only load records that
//are related to the Fund Templates we're looking at.
List<String> FundTemplateTypes = New List<String>();
String SalesOrg;
For(String cmID : fixedFunds.keyset()){
FundTemplateTypes.add(fixedFunds.get(cmID).Fund\_Template\_\_c);
if(SalesOrg == Null){
SalesOrg = fixedFunds.get(cmID).Sales\_Org\_\_c;
}
}
//Calculation of the current week number
Date todaysDate = date.today();
Date todaysDateInstance = date.newInstance(todaysdate.year(), todaysdate.month(), todaysdate.day());
todaysdate.day();
Integer currentyear = todaysdate.year();
Date startDate = date.newInstance(currentyear, 1,1);
Integer numberDaysDue = startDate.daysBetween(todaysDateInstance);
Integer numberOfWeek = math.mod(Integer.valueOf(math.floor((numberDaysDue))/7),52)+1;
//Support Map for NA\_REP\_FixedFund\_A Calculation
Map<String, Decimal> mPrdAmounts = new Map<String, Decimal>();
//Query
AggregateResult\[\] groupedResults = \[SELECT SUM(cgcloud\_\_Deposits\_Approved\_\_c) sumValue,
cgcloud\_\_Anchor\_Product\_\_c,
cgcloud\_\_Fund\_Template\_\_c,
cgcloud\_\_Fund\_Template\_\_r.Name tmplName,
cgcloud\_\_Fund\_Template\_\_r.TPM\_RTRRoutingFundType\_\_c RTRRoutingFundType
FROM cgcloud\_\_Fund\_\_c
WHERE cgcloud\_\_Fund\_Template\_\_r.TPM\_RTRRoutingFundType\_\_c IN :FundTemplateTypes
AND cgcloud\_\_Sales\_Org\_\_c= :SalesOrg
AND cgcloud\_\_Anchor\_Product\_\_c IN :payload.productsfids
AND cgcloud\_\_Anchor\_Account\_\_c IN :payload.accountsfids
AND ((cgcloud\_\_Valid\_From\_\_c <= :inputDateEnd
AND cgcloud\_\_Valid\_From\_\_c >= :inputDateBegin)
OR (cgcloud\_\_Valid\_Thru\_\_c <= :inputDateEnd
AND cgcloud\_\_Valid\_Thru\_\_c >= :inputDateBegin))
GROUP BY cgcloud\_\_Anchor\_Product\_\_c, cgcloud\_\_Fund\_Template\_\_c, cgcloud\_\_Fund\_Template\_\_r.Name, cgcloud\_\_Fund\_Template\_\_r.TPM\_RTRRoutingFundType\_\_c\];
For (AggregateResult ar : groupedResults){
//Check if current product is not already in the map, if so, add to the map with value 0
If(!mPrdAmounts.containsKey(String.valueOf(ar.get('cgcloud\_\_Anchor\_Product\_\_c')))){
mPrdAmounts.put(String.valueOf(ar.get('cgcloud\_\_Anchor\_Product\_\_c')), 0);
}
//Add the sum of the aggregateResult to the existing value in map for current product
mPrdAmounts.put(String.valueOf(ar.get('cgcloud\_\_Anchor\_Product\_\_c')), mPrdAmounts.get(String.valueOf(ar.get('cgcloud\_\_Anchor\_Product\_\_c'))) + Double.valueOf(ar.get('sumValue')));
//Check Fund Template and add KPI
For(String qKPI : fixedFunds.keyset()){
If(String.valueOf(ar.get('RTRRoutingFundType')) == fixedFunds.get(qKPI).Fund\_Template\_\_c){
output.add(new TPM\_RTRReportingWrapper\_AMS.OutputRecord(String.valueOf(ar.get('cgcloud\_\_Anchor\_Product\_\_c')), String.valueOf(fixedFunds.get(qKPI).KPI\_Definition\_\_c), 'Total', Double.valueOf(ar.get('sumValue'))));
}
}
}
//Add NA\_REP\_FixedFund\_A if required
If(fixedFunds.containsKey('NA\_REP\_FixedFund\_A')){
For(String cdr : mPrdAmounts.keyset()){
output.add(new TPM\_RTRReportingWrapper\_AMS.OutputRecord(String.valueOf(cdr), 'NA\_REP\_FixedFund\_A', 'Total', Double.valueOf((mPrdAmounts.get(cdr)\*numberOfWeek/52))));
}
}
return output;
}
}