-3

I am getting the following errors in my code. 'Unexpected token 'public'' and 'Variable does not exist:quoteCurrencyTotal' and the same for quoteCurrency and costCurrency. What am I doing wrong? I am new to apex.

public void getPricesAndReferences(List<Item> items) {
    for (Item i : items) {
        i.pricesMap = new Map<String, Double>();
        i.referencesMap = new Map<String, String>();
        i.totalsMap = new Map <String,Double>();
        i.quoteCurrencyTotal = new map <String, Double>();
        i.costCurrencyTotal = new map <String, Double>();
        i.summedMap = new map <String,Double>(); 
         
        if (MultiQuoteConfigurations.Single_Reference__c) {
            NoOfUnitsForBeaconReference = 1;
            
            for (OpportunityLineItem opportunity_line_item : OpportunityProductsList) {
                String price_unit = (String.isNotBlank(opportunity_line_item.Equipment_Type_Pull__c)) ? opportunity_line_item.Equipment_Type_Pull__c : '-';
                String quote_currency = (String.isNotBlank(opportunity_line_item.Quote_Currency__c)) ? opportunity_line_item.Quote_Currency__c : '-';
                String cost_currency = (String.isNotBlank(opportunity_line_item.Cost_Currency__c)) ? opportunity_line_item.Cost_Currency__c : '-';
                if (i.isRelatedRecord(opportunity_line_item, MultiQuoteConfigurations)) {
                    i.pricesMap.put(opportunity_line_item.Product2.Name + ' ' + price_unit, opportunity_line_item.Unit_Price__c != null ? opportunity_line_item.Unit_Price__c : null);
                    i.referencesMap.put(price_unit, opportunity_line_item.Beacon_Reference_ID__c !=null ? opportunity_line_item.Beacon_Reference_ID__c : '-');                   
                    i.totalsMap.put(price_unit, opportunity_line_item.unit_price_quote_currency__c !=null ? opportunity_line_item.unit_price_quote_currency__c : null);                   
                    i.quoteCurrencyTotal.put(quote_currency + '' + price_unit, opportunity_line_item.Unit_Price_Quote_Currency__c !=null ? opportunity_line_item.Unit_Price_Quote_Currency__c: null);
                    i.costCurrencyTotal.put(cost_currency + '' + price_unit, opportunity_line_item.Unit_Price__c !=null ? opportunity_line_item.Unit_Price__c: null);
                }
                else {
                    i.pricesMap.put(opportunity_line_item.Product2.Name + ' ' + price_unit, null);
                    i.referencesMap.put(price_unit, '-');
                    i.totalsMap.put(price_unit, null);
                    i.quoteCurrencyTotal.put(quote_currency + '' + price_unit,null);
                    i.costCurrencyTotal.put(cost_currency + '' + price_unit,null); 
                }
                 
                public static Map<String,Decimal> sumMaps(Map<String,Decimal>quoteCurrencyTotal, Map<String,Decimal>costCurrencyTotal) {
                    Set<String> keys1 = quoteCurrencyTotal.keySet();
                    Set<String> keys2 = costCurrencyTotal.keySet();
                    keys1.retainAll(keys2);
                    Map<String,Decimal> summedMap = new Map<String,Decimal>();
                    
                    for(String k : keys1) {
                        summedMap.put(k, quoteCurrency.get(k) + costCurrencyTotal.get(k));
                    }
                return summedMap;
            }  
        }
    }
}
}
1
  • 4
    Welcome to SFSE. Please take a moment to read How to Ask and take the tour. Since you are new to Apex, your best way to proceed is to go through the Apex tutorials on Salesforce Trailhead and Salesforce Youtube channels, and make use of the tutorial material available on many other websites. Jan 10, 2022 at 21:06

1 Answer 1

3

You've tried to define a method (sumMaps) inside of another method (getPricesAndReferences), which is not valid syntax. Your use of static here is also problematic (unlikely to do what you think it does).

Methods are always top-level members of a class.

At the very least, you'd need to restructure your class so it resembles this structure:

public class MyClass{
    // Declare class variables
    public Integer myInt1;
    public String myStr1;

    // Define class methods
    // Static methods usually get called by using the class name + . + method name
    // E.g. MyClass.aStaticMethod()
    //   (unless called from within a static initializer or other static method in the
    //   same class)
    public static void aStaticMethod(){
        // some code
    }

    // non-static methods are called "instance" methods
    // you need an "instance" of this class to call these methods (unless it's
    //   being called from another instance method in this class)
    // E.g. MyClass myInstance = new MyClass();
    //      myInstance.aClassMethod();
    public void aClassMethod(){
        // other code
    }
}

Not the answer you're looking for? Browse other questions tagged .