Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
5.2 kB
5
Indexable
Never
public static void paymentBeforeInsert(List<Payment__c> newPayments){
        
        //Create a map for storing Transaction__c.Id and Transaction__c.Base_Currency__c
        Map<Id, String> transactionBaseCurrencyMap = new Map<Id, String>();
        //Iterate through your Payment__c list to get the related Transaction Ids that you can later on use to retrieve the Base Currency
        //
        for (Payment__c payment : newPayments) {
            transactionBaseCurrencyMap.put(payment.Transaction__c, null);
        }

        //If you created a method for retrieving the Base Currency of each related\associated Transactions.
        //You can call that method here
        //If not, then you will need to write your query codes for retrieving the Base Currency of each related\associated Transactions inside this method
        List<Transaction__c> relatedTransactions = [SELECT Id, Base_Currency__c
                                                    FROM Transaction__c
                                                    WHERE Id IN :transactionBaseCurrencyMap.keySet()];

        /*Create a Collection to store the Concatenated value of Transaction__c.Base_Currency__c and Payment__c.Target_Currency__c
        you can use those to filter the Exchange Rate records to only retrieve the exchange rates needed for processing the payments*/
        Set<String> currencyPairs = new Set<String>();

        /*Iterate through your Payment__c list again and access the Target Currency field value for each Payment 
        and concatenate it with the Base Currency field value from the associated Transaction*/
        /*You can use the Map collection that we have created above where you stored the Transaction__c.Id and Transaction__c.Base_Currency to get the related\associated 
        Transaction Base Currency field value inside this loop*/
        for (Payment__c payment : newPayments) {
          String transactionBaseCurrency = transactionBaseCurrencyMap.get(payment.Transaction__c);
          String currencyPair = transactionBaseCurrency + '-' + payment.Target_Currency__c;
             currencyPairs.add(currencyPair);
        }
        
        //If you created a method for retrieving the Exchange Rates
        //You can call that here
        Map<Id, Exchange_Rate__c> exchangeRatesMap = new Map<Id, Exchange_Rate__c>();
        //If not, then you will need to write your query codes for retrieving the Exchange Rates inside this method.

		for (Exchange_Rate__c exchangeRate : [SELECT Id, Rate__c
                                             FROM Exchange_Rate__c]) {
            exchangeRatesMap.put(exchangeRate.Id, exchangeRate);
        }


      /*One last time - iterate through each of the Payment__c records inside your collection and set the 
        and set the Converted Amount.
        All of the collections that were mentioned above will be used here*/
        /*Also add exception handling - when an exchange rate doesn't exist for the Transaction__c.Base_Currency__c-Payment__c.Target_Currency__c
        throw a custom exception using the inner class that you created*/
        /*After throwing the exception, you should catch the exception\error. For each unique error, you will need to create a record in the Error_Log__c object*/
        for (Payment__c payment : newPayments) {
            String transactionBaseCurrency = transactionBaseCurrencyMap.get(payment.Transaction__c);
            String currencyPair = transactionBaseCurrency + '-' + payment.Target_Currency__c;
            currencyPairs.add(currencyPair);
        }

        Map<String, Decimal> exchangeRates = retrieveExchangeRates(currencyPairs);

        for (Payment__c payment : newPayments) {
            String currencyPair = transactionBaseCurrencyMap.get(payment.Transaction__c) + '-' + payment.Target_Currency__c;

            try {
                if (exchangeRates.containsKey(currencyPair)) {
                    Decimal exchangeRate = exchangeRates.get(currencyPair);

                    // Calculate the converted amount using the exchange rate
                    Decimal convertedAmount = payment.Amount__c * exchangeRate;
                    payment.Converted_Amount__c = convertedAmount;

                    // Relate the payment record to the exchange rate
                    // Assuming you have a field on Payment__c object called Exchange_Rate__c
                    payment.Exchange_Rate__c = exchangeRate; // You may need to adjust this based on your schema
                } else {
                    // Throw a custom exception using the inner class that you created
                    throw new PaymentConversionException('Exchange rate not found for currency pair: ' + currencyPair);
                }
            } catch (PaymentConversionException e) {
                // Catch the exception and create a record in the Error_Log__c object
                String errorMsg = 'Error processing payment ' + payment.Id + ': ' + e.getMessage();
                Error_Log__c errorLog = new Error_Log__c(Message__c = errorMsg);
                insert errorLog;
            }
        }
    }
Leave a Comment