0

This question already has an answer here:

global class UpdateExchange_Rate_Advanced_on_Oppty implements Database.Batchable<sObject>,Database.Stateful,schedulable {

    public String query; 

    global UpdateExchange_Rate_Advanced_on_Oppty() {

    }

    global Database.QueryLocator start(Database.BatchableContext BC) {

        if(String.isBlank(query)) {

            query = 'Select Id, CloseDate, CurrencyISOCode,Amount, Amount_USD__c ,Exchange_Rate_Advanced__c from  Opportunity where Exchange_Rate_Advanced__c = null';
        } 

        return Database.getQueryLocator(query); 
    }

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

        Date oppMinDate;
        Date oppMaxDate;
        Set<String> isoCodes = new Set<String>();
        List<Opportunity> newOpps = new List<Opportunity>();
        List<Opportunity> forUpdateOpps = new List<Opportunity>();

        for(Opportunity opp: scope) 
        {
                System.debug('Inside for 1');
                isoCodes.add(opp.CurrencyISOCode);

                if(oppMinDate == null || opp.CloseDate<oppMinDate)
                    oppMinDate = opp.CloseDate;
                if(oppMaxDate == null || opp.CloseDate>oppMaxDate)
                    oppMinDate = opp.CloseDate;    

                newOpps.add(opp);                

        }
        isoCodes.remove(null);

        if(newOpps.size()>0){
            Map<String,List<DatedConversionRate>> conversionRateMap = new Map<String,List<DatedConversionRate>>();
            Date nxtStartDate = date.newInstance(999, 12, 31);

            for(DatedConversionRate dcr:[SELECT ISOCode, ConversionRate, NextStartDate, StartDate FROM DatedConversionRate 
                            Where ISOCode in :isoCodes AND 
                            ( NextStartDate = 9999-12-31 OR 
                                (StartDate <=: oppMinDate AND NextStartDate >=: oppMinDate) OR (StartDate <=: oppMaxDate AND NextStartDate >=: oppMaxDate) 
                            ) ORDER BY StartDate DESC])
            {                    
                if(dcr.NextStartDate == nxtStartDate)
                    conversionRateMap.put(dcr.ISOCode+'9999-12-31',new List<DatedConversionRate>{dcr});
                else if(conversionRateMap.containsKey(dcr.ISOCode))
                    conversionRateMap.get(dcr.ISOCode).add(dcr);
                else
                    conversionRateMap.put(dcr.ISOCode,new List<DatedConversionRate>{dcr});
            }

            for(Opportunity opp: newOpps) {
                Decimal conversionRate;

                if(conversionRateMap.containsKey(opp.CurrencyISOCode)){
                    Boolean matched = false;
                    for(DatedConversionRate dcr : conversionRateMap.get(opp.CurrencyISOCode)){
                        if(dcr.StartDate <= opp.CloseDate && dcr.NextStartDate >= opp.CloseDate){
                            conversionRate = dcr.conversionRate;
                            matched = true;
                            break;
                        }
                    }
                    if(!matched)
                        conversionRate = conversionRateMap.get(opp.CurrencyISOCode+'9999-12-31')[0].ConversionRate;
                }else
                    conversionRate = conversionRateMap.get(opp.CurrencyISOCode+'9999-12-31')[0].ConversionRate;

                opp.Amount_USD__c = 0.00; 
                if(opp.Amount != null)
                    opp.Amount_USD__c = opp.Amount / conversionRate;             

                opp.Exchange_Rate_Advanced__c = conversionRate;   

                forUpdateOpps.add(opp);
            }

            if(forUpdateOpps.size()>0)
            {
                database.update(forUpdateOpps, false);
            }
        }

    }

    global void finish(Database.BatchableContext BC) {

    }

    // Schedule the Batch Apex Class
    global void execute(SchedulableContext sc) {
        Id batchId = Database.executeBatch(new UpdateExchange_Rate_Advanced_on_Oppty(), 10);
    }

}

test class

@isTest(SeeAllData = false)
public  with sharing class Test_UpdateExchange_Rate_Adv_on_Oppty {

    public static testMethod void test(){
      Account acc = new Account(name = 'test Account', CurrencyIsoCode = 'USD', Region__c = 'APAC',   BillingCountry = 'United State', BillingState='WV');
        insert acc;


        Opportunity opp = new Opportunity(Name = 'oppName', AccountId = acc.Id, Forecast_Status__c = 'Funnel', StageName = 'Problem Identified', Region__c = 'APAC',
                                                Registered_Partner_Opportunity__c = 'No Partner', CloseDate = System.Today().addDays(1), Target_Group__c = 'Core');


        insert opp;     



        Test.startTest();

      Id batchId = Database.executeBatch(new UpdateExchange_Rate_Advanced_on_Oppty(), 1);
    Test.stopTest();
    }
}

marked as duplicate by David Reed, Reshma, Pranay Jaiswal, glls, Derek F Jan 28 at 14:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Browse other questions tagged or ask your own question.