1

We have a batch scheduled that runs early morning. It stays stuck in the queue and never executes. Is there any thing wrong in my code

global class BatchAutoRefreshOpportunityRateDeck implements Database.Batchable<sObject> {

global Set<Id> updatedOppIds;


String query = '';
List<Id> rateTableids = new List<Id>();
global BatchAutoRefreshOpportunityRateDeck() {
    updatedOppIds = new Set<Id>();
}

global Database.QueryLocator start(Database.BatchableContext BC) {
    DateTime yesterdayDate = Datetime.now().addDays(-1);
    System.debug(yesterdayDate);
    query = 'select Id, Master_Rate_Deck_XRefs__c, Date_Activated__c, Active__c ';
    query += ' from Rate_Deck_XREF__C';
    query += ' where Date_Activated__c = ' + yesterdayDate.format('yyyy-MM-dd\'T\'HH:mm:ss') + 'Z';
    query += ' and Active__c = true and Master_Rate_Deck_XRefs__r.IsEnabledOpportunityAutoRefresh__c = true';
    return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<Rate_Deck_XREF__C> rateDeckXREFs) {

    Set<Id> masterXREFIds = new Set<Id>();
    Map<Id, Id> masterXREFIdsMap = new Map<Id,Id>();

    for(Rate_Deck_XREF__C rateDeckXREF:rateDeckXREFs) {
        masterXREFIdsMap.put(rateDeckXREF.Master_Rate_Deck_XRefs__c, rateDeckXREF.Id);
    }

    List<OLI_Rate_Table__c> rateTables = [select Id, Rate_Deck__c, Rate_Deck__r.Master_Rate_Deck_XRefs__c from OLI_Rate_Table__c 
                                            where Rate_Deck__r.Master_Rate_Deck_XRefs__c in :masterXREFIdsMap.keySet() 
                                            and Rate_Deck__r.Active__c = false
                                            and Rate_Deck__c != null 
                                            and Opportunity__r.StageName != '6 - Closed/Won'
                                            and Opportunity__r.StageName != '0 - Closed/Lost'
                                            and Opportunity__r.Type in ('New Business','Administrative','Existing Customer')
                                            and Is_Cancelled__c = false
                                            and Is_Selected__c = true
                                            and Transaction_Type__c != 'No Service'
                                            and Transaction_Type__c != 'No Change'
                                            ];

    for(OLI_Rate_Table__c rateTable:rateTables) {
        rateTable.Rate_Deck__c = masterXREFIdsMap.get(rateTable.Rate_Deck__r.Master_Rate_Deck_XRefs__c);
        rateTableids.add(rateTable.Id); 
    }

    if(!rateTables.isEmpty()) {
        update rateTables;
    }
}

global void finish(Database.BatchableContext BC) {

    if(rateTableids.size() > 0){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  
        String[] toAddresses = new String[] {'[email protected]'};  
        mail.setToAddresses(toAddresses); 
        mail.setSubject('Rate Deck Updated On Opportunities');  
        mail.setPlainTextBody('Salesforce Support!!\n\nRate Decks have been updated on the following RateTables:\n\nhttps://xxxx.my.salesforce.com/'+ String.join(rateTableids,'\nhttps://xxxxx.my.salesforce.com/'));  

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    } 

}

}

Here is my scheduler code:

global class BatchAutoRefreshOpptyRateDeckSched implements Schedulable {
global void execute(SchedulableContext SC) {
    BatchAutoRefreshOpportunityRateDeck batchJob = new BatchAutoRefreshOpportunityRateDeck();
    ID batchprocessid = Database.executeBatch(batchJob,1);
}

}

0

You must log in to answer this question.

Browse other questions tagged .