0

I have been able to use the trigger contexts separately in a single method in a handler class before but now i am using the fflib and i am probably just over thinking it. How can i get my method to handle different contexts separately.

This is the method i am using currently on updates. However, for inserts i need it to automatically query for the related account's currency and update the currency on the case to match. I could just duplicate the logic to get it working but i know there is a better way without duplicating this method.

public static void setCurrencyToMatchAccount(list<Case> records, Map<Id, SObject> existingRecords){
    set<Id> casesToMatchAcctCurrency = new set<Id>(); 
    for(Case record : (List<Case>) records){
        Case previous = (case)existingRecords.get(record.Id);
        string recordtypename = Schema.SObjectType.Case.getRecordTypeInfosById().get(record.recordtypeid).getname();
        if(previous.AccountId != record.AccountId && recordtypename.contains('Adjustment') || trigger.isInsert){
            casesToMatchAcctCurrency.add(record.id);
        }
    }
    if(casesToMatchAcctCurrency.size() > 0) {

        map<id,case> casesToUpdateMap = new map<id,case>();
        set<Id> caseIdsWithAdjLineItems = new set<Id>();
       for(case case0 : [select id, CurrencyIsoCode, recordtypeid, AccountId, Account.MS_Billing_Currency__c, Adj_Line_Item_Count__c FROM Case where id IN: casesToMatchAcctCurrency]) {
           if(case0.AccountId != null) {
               if(case0.Account.MS_Billing_Currency__c == 'EUR' || case0.Account.MS_Billing_Currency__c == 'CAD') {
                    case0.CurrencyISOCode = case0.Account.MS_Billing_Currency__c;
                } else {
                    case0.CurrencyISOCode = 'USD';
                }

                if(case0.Adj_Line_Item_Count__c > 0) {
                    caseIdsWithAdjLineItems.add(case0.Id);
                }
                casesToUpdateMap.put(case0.id,case0);
            }
        }
        database.update(casesToUpdateMap.values());

        list<Adjustment_Line_Items__c> adjLineItemsToUpdate = new list<Adjustment_Line_Items__c>();
        for(Adjustment_Line_Items__c ali : [select id, CurrencyIsoCode, Case__c from Adjustment_Line_Items__c where Case__c IN: caseIdsWithAdjLineItems]) {
            if(ali.CurrencyIsoCode != casesToUpdateMap.get(ali.Case__c).CurrencyIsoCode) {
                ali.CurrencyIsoCode = casesToUpdateMap.get(ali.Case__c).CurrencyIsoCode;
                adjLineItemsToUpdate.add(ali);
            }
        }
        database.update(adjLineItemsToUpdate);
    }
}

Even just a link to an example will probably get me what i need but i have not been able to find a solid one that was easily readable.

0

Realized i still have access to basic trigger context info -

    public static void setCurrencyToMatchAccount(list<Case> records, Map<Id, SObject> existingRecords){
    set<Id> casesToMatchAcctCurrency = new set<Id>(); 

    for(Case record : (List<Case>) records){
        Case previous = new Case();
        if(trigger.isUpdate) {
            previous = (case)existingRecords.get(record.Id);
        }
        string recordtypename = Schema.SObjectType.Case.getRecordTypeInfosById().get(record.recordtypeid).getname();
        if((previous.AccountId != record.AccountId || trigger.isInsert) && recordtypename.contains('Adjustment') ){
            casesToMatchAcctCurrency.add(record.id);
        }
    }
    if(casesToMatchAcctCurrency.size() > 0) {

        map<id,case> casesToUpdateMap = new map<id,case>();
        set<Id> caseIdsWithAdjLineItems = new set<Id>();
       for(case case0 : [select id, CurrencyIsoCode, recordtypeid, AccountId, Account.MS_Billing_Currency__c, Adj_Line_Item_Count__c FROM Case where id IN: casesToMatchAcctCurrency]) {
           if(case0.AccountId != null) {
               if(case0.Account.MS_Billing_Currency__c == 'EUR' || case0.Account.MS_Billing_Currency__c == 'CAD') {
                    case0.CurrencyISOCode = case0.Account.MS_Billing_Currency__c;
                } else {
                    case0.CurrencyISOCode = 'USD';
                }

                if(case0.Adj_Line_Item_Count__c > 0) {
                    caseIdsWithAdjLineItems.add(case0.Id);
                }
                casesToUpdateMap.put(case0.id,case0);
            }
        }
        database.update(casesToUpdateMap.values());
        list<Adjustment_Line_Items__c> adjLineItemsToUpdate = new list<Adjustment_Line_Items__c>();
        if(trigger.isUpdate) {
            for(Adjustment_Line_Items__c ali : [select id, CurrencyIsoCode, Case__c from Adjustment_Line_Items__c where Case__c IN: caseIdsWithAdjLineItems]) {
                if(ali.CurrencyIsoCode != casesToUpdateMap.get(ali.Case__c).CurrencyIsoCode) {
                    ali.CurrencyIsoCode = casesToUpdateMap.get(ali.Case__c).CurrencyIsoCode;
                    adjLineItemsToUpdate.add(ali);
                }
            }
        }
        database.update(adjLineItemsToUpdate);
    }
}

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.