---
schema_version: "1"
id: a70e15a0eeba07f6e196ecbbbc4636f49cdfb4c1d18133257866aab285b44f66
title: TPM_RTRSalesforceMonthlyMeasures_AMS (Base Class)
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/cgcloud_rtr_dev_guide/tpm_rtr_class_rtrsalesforcemonthlymeasures2_ams.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.cgcloud_rtr_dev_guide.meta/cgcloud_rtr_dev_guide/tpm_rtr_class_rtrsalesforcemonthlymeasures2_ams.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: e4b5316292ecd4ac2ad21fdc8d1b524ab934137997a803bf511e3eff508883f2
description: TPM_RTRSalesforceMonthlyMeasures_AMS is the second base class that's used for code-routing and is required to retrieve the information by a Salesforce REST endpoint (/services/apexrest/RTRSalesforceMonthlyMeasures).
---
# TPM\_RTRSalesforceMonthlyMeasures\_AMS (Base Class)

TPM\_RTRSalesforceMonthlyMeasures\_AMS is the second base class that's used for code-routing and is required to retrieve the information by a Salesforce REST endpoint (/services/apexrest/RTRSalesforceMonthlyMeasures).

@RestResource(urlMapping='/RTRSalesforceMonthlyMeasures')
global with sharing class TPM\_RTRSalesforceMonthlyMeasures\_AMS {
@HttpGet
global static String doGet(){
return 'Hello world!';
}
@HttpPost
global static List<TPM\_RTRReportingWrapper\_AMS.OutputRecord> doPost(){
RestContext.response.statuscode = 200;

These are the code details:

1. The payload is received, and is then turned into an object of our wrapper class. The payload contains the KPIs that are defined in the report configuration.
    
    String requestBody = RestContext.request.requestBody.toString();
    TPM\_RTRReportingWrapper\_AMS.InputPayload payload = (TPM\_RTRReportingWrapper\_AMS.InputPayload) JSON.deserialize(requestBody, TPM\_RTRReportingWrapper\_AMS.InputPayload.class);
    System.debug(payload);
    if(payload.error == true) {
    RestContext.response.statuscode = 500;
    return null;
    }
2. The integration user runs the base classes, not the user who opens the report. As a result, ensure that you query an account on the payload that the user selected to retrieve the actual user's sales org.
    
    //Get Sales Org of the user who initiated the report
    Account acc = \[SELECT Id, cgcloud\_\_Sales\_Org\_\_c FROM Account WHERE Id IN :payload.accountsfids LIMIT 1\];
    String userSFOrg = acc.cgcloud\_\_Sales\_Org\_\_c;
3. Helper maps and lists are created.
    
    //Helper Maps/List
    List<TPM\_RTRReportingWrapper\_AMS.OutputRecord> output = new List<TPM\_RTRReportingWrapper\_AMS.OutputRecord>();
    Map<String, TPM\_RTRRouting\_\_mdt> categoryKPIs = New Map<String, TPM\_RTRRouting\_\_mdt>();
    Map<String, TPM\_RTRRouting\_\_mdt> brandKPIs = New Map<String, TPM\_RTRRouting\_\_mdt>();
4. The dates from the payload are converted to a workable format.
    
    //Loading and transforming the dates selected in the filter
    Date inputDateBegin = Date.newInstance(payload.periodmonth.year, payload.periodmonth.start+1, 1);
    Date inputDateEnd = inputDateBegin.addMonths(payload.periodmonth.total).addDays(-1);
5. The Apex class contains use case-specific code. Here, you can query all custom metadata records that were defined earlier and that belong to the sales org of the user. Then you can check whether the payload contains any KPIs in the custom metadata records. If it does, verify the level on which those KPIs are available and then add them to a dedicated map (category KPIs or brand KPIs map).
    
    //Check against custom metadata if payload contains relevant KPIs and sort them
    For(TPM\_RTRRouting\_\_mdt kpi : \[SELECT Id, KPI\_Definition\_\_c, KPI\_Level\_\_c, Fund\_Template\_\_c, Sales\_Org\_\_c FROM TPM\_RTRRouting\_\_mdt WHERE Sales\_Org\_\_c = :userSFOrg\]){
    If(payload.kpis.contains(kpi.KPI\_Definition\_\_c)){
    If(kpi.KPI\_Level\_\_c == 'Category'){
    categoryKPIs.put(kpi.KPI\_Definition\_\_c, kpi);
    }
    Else If(kpi.KPI\_Level\_\_c == 'Brand'){
    brandKPIs.put(kpi.KPI\_Definition\_\_c, kpi);
    }
    }
    }
6. You can check whether the two maps (brandKPIs and categoryKPIs) contain any values — if not, nothing happens. If they do, the actual class where all the logic happens is called and parameters such as the payload are passed.
    
    //Run logic
    try{
    If(!categoryKPIs.isEmpty()){
    TPM\_RTRFixedFunds\_AMS.getCategory(inputDateBegin, inputDateEnd, payload, output, categoryKPIs);
    }
    If(!brandKPIs.isEmpty()){
    TPM\_RTRFixedFunds\_AMS.getBrand(inputDateBegin, inputDateEnd, payload, output, brandKPIs);
    }
    }
    catch(Exception ex){
    RestContext.response.statuscode = 400;
    System.debug(ex);
    }
    return output;
    }
    }