• Alexander Boros
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies

Hi all,

I wonder if you can help me. Very recently we started having issues with an Apex script that is used to populated the Montly obtainment value in a daily report.

The value is supposed to let the Sales rep know their sales amount so far this month. However, the value that it calculates flunctuates wildly from working correctly, to being off by thousands.

The script itself was originally written several years ago by an agency that is no longer around. We are trying to get a better understanding on what it exactly does so we can implement a fix. 

The Apex code being used is found below.

global with sharing class Forecast_Batch implements Database.Batchable<sObject>, Database.Stateful{
    Map<String, Decimal> amountMap = new Map<String, Decimal>();
    List<Id> accountOwnerIds = new List<Id>();

        global Database.QueryLocator start(Database.BatchableContext BC)
        {
            
            return Database.getQueryLocator([SELECT Id,OwnerId FROM Account]); //Query all accounts for ID and OwnerId
            
        }

    global void execute(Database.BatchableContext BC, List<SObject> scope)
    {        
        Map<Id, Map<String,Decimal>> accountAmountMap = new Map<Id,Map<String,Decimal>>();

        //List<Id> accountOwnerIds = new List<Id>();
        Set<Id> accountIds = new Set<Id>();
        
        system.debug('+++ SCOPE SIZE ' + scope.size());
        
        for (SObject s : scope) {
            
            Account acc = (Account)s;
            if(!accountIds.contains(acc.Id)){
                accountIds.add(acc.Id);
            }
            
        }
        
        
        for (Account acc : [SELECT OwnerId,(SELECT Name,SA_Net_Amount__c,InvoiceDate__c,OwnerDetails__c,Inv_Yr__c FROM Invoice__r WHERE OwnerDetails__c != null and SA_Net_Amount__c !=null ) FROM Account WHERE Id in : accountIds]) {
            
            for (Invoice__c inv : acc.Invoice__r) {
                
                System.debug(inv.name);
          Decimal amount = 0;
                
          if(amountMap.containsKey(inv.OwnerDetails__c)){
              amount = amountMap.get(inv.OwnerDetails__c);
          }
    
          amountMap.put(inv.OwnerDetails__c, inv.SA_Net_Amount__c + amount);
      }
            //accountAmountMap.put(acc.Id, amountMap);
            accountOwnerIds.add(acc.OwnerID);
            
        }
        System.debug(accountOwnerIds);
        


        
    }  

    global void finish(Database.BatchableContext BC){
        
        // Query list of forecast details record based on Account's owner Id
        List<H_Forecast_Details__c> forecastdetails = new List<H_Forecast_Details__c>();

        // Iterate accounts in for loop
        // iterate forecast details record
        // get invoice amount based on account id and forcast month.
        // update in forecast detail record.
        for (H_Forecast_Details__c forecastDet : [SELECT ID,Name,Month_Obtainment__c,OwnerID__c,FDOwnerDets__c from H_Forecast_Details__c where OwnerID__c IN : accountOwnerIds]) {
            if(amountMap.containsKey(forecastDet.FDOwnerDets__c)){
                Decimal amount  = amountMap.get(forecastDet.FDOwnerDets__c);  
                forecastDet.Month_Obtainment__c = amount;
                System.debug('match');
                System.debug(amount);

    forecastdetails.add(forecastDet);
            }
        }

  System.debug(forecastdetails);
  if(forecastdetails!=null&&forecastdetails.size()>0){
          update forecastdetails;
  }
    }
}

Kind regards,

Alex

Hi all,

I wonder if you can help me. Very recently we started having issues with an Apex script that is used to populated the Montly obtainment value in a daily report.

The value is supposed to let the Sales rep know their sales amount so far this month. However, the value that it calculates flunctuates wildly from working correctly, to being off by thousands.

The script itself was originally written several years ago by an agency that is no longer around. We are trying to get a better understanding on what it exactly does so we can implement a fix. 

The Apex code being used is found below.

global with sharing class Forecast_Batch implements Database.Batchable<sObject>, Database.Stateful{
    Map<String, Decimal> amountMap = new Map<String, Decimal>();
    List<Id> accountOwnerIds = new List<Id>();

        global Database.QueryLocator start(Database.BatchableContext BC)
        {
            
            return Database.getQueryLocator([SELECT Id,OwnerId FROM Account]); //Query all accounts for ID and OwnerId
            
        }

    global void execute(Database.BatchableContext BC, List<SObject> scope)
    {        
        Map<Id, Map<String,Decimal>> accountAmountMap = new Map<Id,Map<String,Decimal>>();

        //List<Id> accountOwnerIds = new List<Id>();
        Set<Id> accountIds = new Set<Id>();
        
        system.debug('+++ SCOPE SIZE ' + scope.size());
        
        for (SObject s : scope) {
            
            Account acc = (Account)s;
            if(!accountIds.contains(acc.Id)){
                accountIds.add(acc.Id);
            }
            
        }
        
        
        for (Account acc : [SELECT OwnerId,(SELECT Name,SA_Net_Amount__c,InvoiceDate__c,OwnerDetails__c,Inv_Yr__c FROM Invoice__r WHERE OwnerDetails__c != null and SA_Net_Amount__c !=null ) FROM Account WHERE Id in : accountIds]) {
            
            for (Invoice__c inv : acc.Invoice__r) {
                
                System.debug(inv.name);
          Decimal amount = 0;
                
          if(amountMap.containsKey(inv.OwnerDetails__c)){
              amount = amountMap.get(inv.OwnerDetails__c);
          }
    
          amountMap.put(inv.OwnerDetails__c, inv.SA_Net_Amount__c + amount);
      }
            //accountAmountMap.put(acc.Id, amountMap);
            accountOwnerIds.add(acc.OwnerID);
            
        }
        System.debug(accountOwnerIds);
        


        
    }  

    global void finish(Database.BatchableContext BC){
        
        // Query list of forecast details record based on Account's owner Id
        List<H_Forecast_Details__c> forecastdetails = new List<H_Forecast_Details__c>();

        // Iterate accounts in for loop
        // iterate forecast details record
        // get invoice amount based on account id and forcast month.
        // update in forecast detail record.
        for (H_Forecast_Details__c forecastDet : [SELECT ID,Name,Month_Obtainment__c,OwnerID__c,FDOwnerDets__c from H_Forecast_Details__c where OwnerID__c IN : accountOwnerIds]) {
            if(amountMap.containsKey(forecastDet.FDOwnerDets__c)){
                Decimal amount  = amountMap.get(forecastDet.FDOwnerDets__c);  
                forecastDet.Month_Obtainment__c = amount;
                System.debug('match');
                System.debug(amount);

    forecastdetails.add(forecastDet);
            }
        }

  System.debug(forecastdetails);
  if(forecastdetails!=null&&forecastdetails.size()>0){
          update forecastdetails;
  }
    }
}

Kind regards,

Alex