0

I am just click on custom button then call the controller and run the batch but show the error occour during run the batch.and show below error. How should i do this? Apex class

global class CalculateRevenuesController implements Database.Batchable<sObject>,  Database.Stateful{
Date lastYearWithTodaysDate;
Date lastDate;
Date currentYearStartDate;
Date lastYearStartDate;
Date lastYearwithBeforemonthEndDate;
Date lastYearendDate;
Date beforeLastyearStartDate;
Date beforeLastyearEndDate;
Map<Id, Decimal> ytdRevenueForThisYearValueMap = new Map<Id, Decimal>();
Map<Id, Decimal> ytdRevenueForLastYearValueMap = new Map<Id, Decimal>();
Map<Id, Decimal> revenueLastYearValueMap = new Map<Id, Decimal>();
Map<Id, Decimal> revenueBeforeLastYearValueMap = new Map<Id, Decimal>();

Set<Id> allAccountIdSet = new Set<Id>();

global CalculateRevenuesController(){
    Date selectedDate =  Date.today().addMonths(-1); //Give your date
    Date firstDate = selectedDate.toStartOfMonth();
    lastDate = firstDate.addDays(date.daysInMonth(selectedDate.year() , selectedDate.month())  - 1);
    lastYearWithTodaysDate = system.today().addYears(-1);
    Integer currentYear = system.today().year();
    currentYearStartDate = date.newinstance(currentYear, 1, 1);
    lastYearStartDate = date.newinstance(currentYear - 1, 1, 1);
    lastYearwithBeforemonthEndDate = lastDate.addYears(-1);
    lastYearendDate = date.newinstance(currentYear-1,12,31);
    beforeLastyearStartDate = date.newinstance(currentYear - 2, 1, 1);
    beforeLastyearEndDate = date.newinstance(currentYear - 2, 12, 31);
}

global Database.QueryLocator start(Database.BatchableContext BC){
    String query = 'Select Id, Account__c, Invoice_Date__c, Revenue__c from Revenue__c';
    return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<sObject> scope){




    List<Revenue__c> revenueList = scope;
    /*for(Revenue__c rev : revenueList){
        ytdRevenueForThisYearMap.put(rev.Account__c,rev); 
        ytdRevenueForLastYearMap.put(rev.Account__c,rev); 
        revenueLastYearMap.put(rev.Account__c,rev); 
        revenueBeforeLastYearMap.put(rev.Account__c,rev);
    }*/
    Map<Id, List<Revenue__c>> ytdRevenueMap = new Map<Id, List<Revenue__c>>();
    Set<Id> accountIdSet = new Set<Id>();
    for(Revenue__c rev : revenueList){
        accountIdSet.add(rev.Account__c);
        if(ytdRevenueMap.containskey(rev.Account__c)){
            //List<Revenue__c> tempRevenue = ytdRevenueMap.get(rev.account__c);
            //tempRevenue.add(rev);
            //ytdRevenueMap.put(rev.account__c,tempRevenue);
            ytdRevenueMap.get(rev.account__c).add(rev);
        }else{
            //tempRevenue = new List<Revenue__c>(); 
            //tempRevenue.add(rev);
            //ytdRevenueMap.put(rev.account__c,tempRevenue);  
            ytdRevenueMap.put(rev.account__c,new List<Revenue__c>());   
            ytdRevenueMap.get(rev.account__c).add(rev);
        }
    }
    for(Id accId : accountIdSet){
        for(Revenue__c rev : ytdRevenueMap.get(accId)){
            if(rev.Account__c == null || rev.Invoice_Date__c == null || rev.Revenue__c == null){
                continue;
            }
            if(rev.Revenue__c != null){
                // Calculate YTD Revenue for this year..
                if(rev.Invoice_Date__c <= lastDate && rev.Invoice_Date__c >= currentYearStartDate){
                    if(ytdRevenueForThisYearValueMap.get(rev.Account__c) != null){

                        Decimal oldRevenue = ytdRevenueForThisYearValueMap.get(rev.Account__c);
                        oldRevenue = oldRevenue + rev.Revenue__c;
                        ytdRevenueForThisYearValueMap.put(rev.Account__c, oldRevenue );
                        system.debug('==oldRevenue YTD===>'+oldRevenue);
                    } else {
                        ytdRevenueForThisYearValueMap.put(rev.Account__c, rev.Revenue__c);
                        system.debug('==oldRevenue YTD else===>'+rev.Revenue__c);
                    }
                }

                // Calculate YTD Revenue for last year..
                if(rev.Invoice_Date__c <= lastYearwithBeforemonthEndDate && rev.Invoice_Date__c >= lastYearStartDate){
                    if(ytdRevenueForLastYearValueMap.get(rev.Account__c) != null){

                        Decimal oldRevenue = ytdRevenueForLastYearValueMap.get(rev.Account__c);
                        oldRevenue = oldRevenue + rev.Revenue__c;
                        ytdRevenueForLastYearValueMap.put(rev.Account__c, oldRevenue );
                        system.debug('==oldRevenue YTD===>'+oldRevenue);
                    } else {
                        ytdRevenueForLastYearValueMap.put(rev.Account__c, rev.Revenue__c);
                        system.debug('==oldRevenue YTD else===>'+rev.Revenue__c);
                    }



                }

                // Calculate Revenue for Last Year.
                if(rev.Invoice_Date__c >= lastYearStartDate && rev.Invoice_Date__c <= lastYearendDate){
                    if(revenueLastYearValueMap.get(rev.Account__c) != null){

                        Decimal oldRevenue = revenueLastYearValueMap.get(rev.Account__c);
                        oldRevenue = oldRevenue + rev.Revenue__c;
                        revenueLastYearValueMap.put(rev.Account__c, oldRevenue );
                        system.debug('==oldRevenue YTD===>'+oldRevenue);
                    } else {
                        revenueLastYearValueMap.put(rev.Account__c, rev.Revenue__c);
                        system.debug('==oldRevenue YTD else===>'+rev.Revenue__c);
                    }


                }

                // Calculate Revenue for Before Last Year.
                if(rev.Invoice_Date__c >= beforeLastyearStartDate && rev.Invoice_Date__c <= beforeLastyearEndDate){
                    if(revenueBeforeLastYearValueMap.get(rev.Account__c) != null){

                        Decimal oldRevenue = revenueBeforeLastYearValueMap.get(rev.Account__c);
                        oldRevenue = oldRevenue + rev.Revenue__c;
                        revenueBeforeLastYearValueMap.put(rev.Account__c, oldRevenue );
                        system.debug('==oldRevenue YTD===>'+oldRevenue);
                    } else {
                        revenueBeforeLastYearValueMap.put(rev.Account__c, rev.Revenue__c);
                        system.debug('==oldRevenue YTD else===>'+rev.Revenue__c);
                    }


                }
            }
        }
    }



    Set<Id> principalIdSet = new Set<Id>();
    List<Account> accountsToUpdate = new List<Account>();
    for(Account acc : [Select Id, PRINCIPAL_Name__c from Account where Id in : accountIdSet]){
        acc.YTD_Revenue_Last_Year__c = ytdRevenueForLastYearValueMap.get(acc.Id);
        acc.YTD_Revenue_This_Year__c = ytdRevenueForThisYearValueMap.get(acc.Id);
        acc.Revenue_Last_Year__c = revenueLastYearValueMap.get(acc.Id);
        acc.Revenue_Year_Before_Last__c = revenueBeforeLastYearValueMap.get(acc.Id);

        accountsToUpdate.add(acc);
        allAccountIdSet.add(acc.Id);
    }

    update accountsToUpdate;

}

global void finish(Database.BatchableContext BC){

    AsyncApexJob a = [Select Id, Status, ExtendedStatus, NumberOfErrors, JobItemsProcessed,
       TotalJobItems, CreatedBy.Email
       from AsyncApexJob where Id =:BC.getJobId()];

    String body = 'There were '+ a.NumberOfErrors + ' errors when processing the revenue calculatins job.<br/>' ;
    if(a.NumberOfErrors > 0){
        body += 'The error message was : ' + a.ExtendedStatus + '.<br/>' ;
        body += 'Retry the operation. If you run in to the same error, consult your system administrator.';
    }

    CalculateRevenuesForPrincipalsController batchable = new CalculateRevenuesForPrincipalsController(allAccountIdSet, body, a.Status, a.CreatedBy.Email);
    Database.executeBatch(batchable);
}

}

enter image description here

  • n.b. (1) "Controller" is not a helpful part of the class name as it implies a VF controller. I tend to use the word "Batchable" in my batch class names; (2) Unless you are doing a managed package, you don't need global access modifier - public will do (3) try-catch in your execute() method will help identify stacktrace and record that error occurred on - you can put that in a stateful variable and report on it in finish() – cropredy Aug 28 '15 at 19:50
0

First, seeing that the line number the exception occurs at is row 35 in your log isn't helpful information since we can't tell which line number in your code that is when viewing it here, so it would have been wise to mark where that is in the code you've posted.

That being said, check your workflow and approval processes for Accounts. That's what your error message is telling you is causing your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.