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).

1@RestResource(urlMapping='/RTRSalesforceMonthlyMeasures')
2global with sharing class TPM_RTRSalesforceMonthlyMeasures_AMS {
3@HttpGet
4global static String doGet(){
5return 'Hello world!';
6}
7@HttpPost
8global static List<TPM_RTRReportingWrapper_AMS.OutputRecord> doPost(){
9RestContext.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.

    1String requestBody = RestContext.request.requestBody.toString();
    2TPM_RTRReportingWrapper_AMS.InputPayload payload = (TPM_RTRReportingWrapper_AMS.InputPayload) JSON.deserialize(requestBody, TPM_RTRReportingWrapper_AMS.InputPayload.class);
    3System.debug(payload);
    4if(payload.error == true) {
    5RestContext.response.statuscode = 500;
    6return null;
    7}
  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.

    1//Get Sales Org of the user who initiated the report
    2Account acc = [SELECT Id, cgcloud__Sales_Org__c FROM Account WHERE Id IN :payload.accountsfids LIMIT 1];
    3String userSFOrg = acc.cgcloud__Sales_Org__c;
  3. Helper maps and lists are created.

    1//Helper Maps/List
    2List<TPM_RTRReportingWrapper_AMS.OutputRecord> output = new List<TPM_RTRReportingWrapper_AMS.OutputRecord>();
    3Map<String, TPM_RTRRouting__mdt> categoryKPIs = New Map<String, TPM_RTRRouting__mdt>();
    4Map<String, TPM_RTRRouting__mdt> brandKPIs = New Map<String, TPM_RTRRouting__mdt>();
  4. The dates from the payload are converted to a workable format.

    1//Loading and transforming the dates selected in the filter
    2Date inputDateBegin = Date.newInstance(payload.periodmonth.year, payload.periodmonth.start+1, 1);
    3Date 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).

    1//Check against custom metadata if payload contains relevant KPIs and sort them
    2For(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]){
    3If(payload.kpis.contains(kpi.KPI_Definition__c)){
    4If(kpi.KPI_Level__c == 'Category'){
    5categoryKPIs.put(kpi.KPI_Definition__c, kpi);
    6}
    7Else If(kpi.KPI_Level__c == 'Brand'){
    8brandKPIs.put(kpi.KPI_Definition__c, kpi);
    9}
    10}
    11}
  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.

    1//Run logic
    2try{
    3If(!categoryKPIs.isEmpty()){
    4TPM_RTRFixedFunds_AMS.getCategory(inputDateBegin, inputDateEnd, payload, output, categoryKPIs);
    5}
    6If(!brandKPIs.isEmpty()){
    7TPM_RTRFixedFunds_AMS.getBrand(inputDateBegin, inputDateEnd, payload, output, brandKPIs);
    8}
    9}
    10catch(Exception ex){
    11RestContext.response.statuscode = 400;
    12System.debug(ex);
    13}
    14return output;
    15}
    16}