function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
surbhi narulasurbhi narula 

Getting Error "Global variable must be contained inside of a global class"

I have a class which is global and extending another class(which is managed packaged file) :-

//to enable shipping extension: browse to CC_Admin->Storefront->Your store->Integrations->Shipping->Shipping Integration Settings->
//set Shipping Integration API Class = c.cc_avid_ShippingAndHandling.
global with sharing class cc_b_ShippingAndHandling extends ccrz.cc_api_ShippingAndHandling {
    double cartWeight;
    double insuredValue;
 
    public boolean isBypassShipping(String zipCode, String stateCode, String countryCode, Id cartId, String storeName) {
        double weight = 0.0;
        insuredValue = 0;
        list<ccrz__E_CartItem__c> items = [Select c.ccrz__cartItemType__c, c.ccrz__SubAmount__c, c.ccrz__ParentCartItem__r.ccrz__SubAmount__c, c.ccrz__ParentCartItem__c, c.ccrz__Quantity__c, c.ccrz__Product__r.ccrz__ShippingWeight__c, c.ccrz__Product__c, c.Id From ccrz__E_CartItem__c c where c.ccrz__Cart__c = :cartId ];
        for(ccrz__E_CartItem__c item : items) {
            if(item.ccrz__Product__r.ccrz__ShippingWeight__c != null)
            {  
                weight += (item.ccrz__Product__r.ccrz__ShippingWeight__c * item.ccrz__Quantity__c);
                if('Minor'.equals(item.ccrz__cartItemType__c))
                    insuredValue += item.ccrz__ParentCartItem__r.ccrz__SubAmount__c;
                else
                    insuredValue += item.ccrz__SubAmount__c;
            }
        }
        if(this.cartWeight == null){
            this.cartWeight = weight;
        }
        if(weight > 0.0) {
            return false;
        } else {
            return true;
        }
    }
 
    private double getWeight(String zipCode, String stateCode, String countryCode, Id cartId, String storeName){
        if(this.cartWeight == null)
            isBypassShipping(zipCode, stateCode, countryCode, cartId, storeName);
        return this.cartWeight;
    }
    
    private List<ccrz.cc_ctrl_hlpr_ShippingOption> sortBasedOnPrice(List<ccrz.cc_ctrl_hlpr_ShippingOption> sourceList) {
        Map<Double, List<ccrz.cc_ctrl_hlpr_ShippingOption>> optionMap = new Map<Double, List<ccrz.cc_ctrl_hlpr_ShippingOption>>();
        List<ccrz.cc_ctrl_hlpr_ShippingOption> sortedList = new List<ccrz.cc_ctrl_hlpr_ShippingOption>();
       
        //Create a map for sorting
        for(ccrz.cc_ctrl_hlpr_ShippingOption ob : sourceList){
            if(optionMap.get(ob.price) == null){
                optionMap.put(ob.price, new List<ccrz.cc_ctrl_hlpr_ShippingOption>());
            }
            optionMap.get(ob.price).add(ob);
        }     
    
        //Sort the keys
        List<Double> keys = new List<Double>(optionMap.keySet());
        keys.sort();
       
        //Prepare final list
        for(Double key : keys){
            sortedList.addAll(optionMap.get(key));
        }  
        System.debug('sortedList: '+sortedList);
        return sortedList;
    }
               
        global override List<ccrz.cc_ctrl_hlpr_ShippingOption> getShippingOptions(String zipCode, String stateCode, String countryCode, Id cartId, String storeName) {
 
             if(isBypassShipping(zipCode, stateCode, countryCode, cartId, storeName)) {
            //return an empty List<ccrz.cc_ctrl_hlpr_ShippingOption> to hide Shipment Information block
            return new List<ccrz.cc_ctrl_hlpr_ShippingOption>();
        } else {
            List<ccrz__E_Cart__c> cart = [select ccrz__ShipTo__R.ccrz__City__c, ccrz__ShipTo__R.ccrz__AddressFirstLine__c, ccrz__ShipTo__R.ccrz__CountryISOCode__c from ccrz__E_Cart__c where id = :cartId];
            cc_country_region_settings__c settings = cc_country_region_settings__c.getAll().get(countryCode);
            String avidRegion = 'Avid';
            if(settings != null)
                avidRegion = cc_country_region_settings__c.getAll().get(countryCode).AROMA_Region__c;               
                ccrz.cc_ctrl_hlpr_ShippingOption resultShippingOption = new ccrz.cc_ctrl_hlpr_ShippingOption();
                                                                resultList['currencyCode'] = 'INR';
                                                                resultList['discount'] = '0.5';
                                                                resultList['discountedShipCost'] = '0.10';
                                                                resultList['price'] = '100';
                                                                resultList['provider'] = 'test';
                                                                resultList['serviceName'] = 'testService';
                                                                resultList['systemReference'] = 'testReference';
                                                                resultList['uid'] = 'testUid';
                                                                resultList['uniqueId'] = 'testUniqueId';
                List<ccrz.cc_ctrl_hlpr_ShippingOption> resultListTest;
            try{
              
                System.debug(LoggingLevel.INFO, 'Using shipping options for storefront : ' + storeName);
                //try{
                //resultList = getShippingOptionsFromAvidFedExService(getWeight(zipCode, stateCode, countryCode, cartId, storeName), this.insuredValue, cart.get(0).ccrz__ShipTo__r.ccrz__AddressFirstLine__c, cart.get(0).ccrz__ShipTo__r.ccrz__City__c, zipCode, stateCode, countryCode, UserInfo.getDefaultCurrency(), cartId, avidRegion);
 
                }
        }
   
   
    global List<ccrz.cc_ctrl_hlpr_ShippingOption> fetchShippingOptions(String zipCode, String stateCode, String countryCode, Id cartId, String storeName) {
        if(isBypassShipping(zipCode, stateCode, countryCode, cartId, storeName)) {
            //return an empty List<ccrz.cc_ctrl_hlpr_ShippingOption> to hide Shipment Information block
            return new List<ccrz.cc_ctrl_hlpr_ShippingOption>();
        } else {
            List<ccrz__E_Cart__c> cart = [select ccrz__ShipTo__R.ccrz__City__c, ccrz__ShipTo__R.ccrz__AddressFirstLine__c, ccrz__ShipTo__R.ccrz__CountryISOCode__c from ccrz__E_Cart__c where id = :cartId];
            cc_country_region_settings__c settings = cc_country_region_settings__c.getAll().get(countryCode);
            String avidRegion = 'Avid';
            if(settings != null)
                avidRegion = cc_country_region_settings__c.getAll().get(countryCode).AROMA_Region__c;               
                List<ccrz.cc_ctrl_hlpr_ShippingOption> resultList = new List<ccrz.cc_ctrl_hlpr_ShippingOption>();
                List<ccrz.cc_ctrl_hlpr_ShippingOption> resultListTest;
            try{
               //List<ccrz.cc_ctrl_hlpr_ShippingOption> resultList = new List<ccrz.cc_ctrl_hlpr_ShippingOption>();
                //List<ccrz.cc_ctrl_hlpr_ShippingOption> resultListTest;
                System.debug(LoggingLevel.INFO, 'Using shipping options for storefront : ' + storeName);
                try{
                //resultList = getShippingOptionsFromAvidFedExService(getWeight(zipCode, stateCode, countryCode, cartId, storeName), this.insuredValue, cart.get(0).ccrz__ShipTo__r.ccrz__AddressFirstLine__c, cart.get(0).ccrz__ShipTo__r.ccrz__City__c, zipCode, stateCode, countryCode, UserInfo.getDefaultCurrency(), cartId, avidRegion);
                
                //Default shipping options
                /*if(resultList == null || resultList.size() == 0){
                              resultList.add(buildDefaultShippingOption(countryCode));
                }*/
                
                    cc_avid_TaxExemptCountry__c countryTaxInfo = cc_avid_api_TaxCalculation.getCountryTaxInfo(cart.get(0).ccrz__ShipTo__R.ccrz__CountryISOCode__c);
                    if(countryTaxInfo != null && 'GROSS'.equalsIgnoreCase(countryTaxInfo.PricingType__c)){
                        System.debug(LoggingLevel.INFO, '---------Gross Pricing--------');
                        Decimal shipTaxMultiplier = 1 + countryTaxInfo.VATShipping__c;
                        for(ccrz.cc_ctrl_hlpr_ShippingOption result : resultList){
                            System.debug(LoggingLevel.INFO, '---------original ship price--------' + result.price);
                            result.price = cc_avid_api_TaxCalculation.roundToTwoDecimals(result.price * shipTaxMultiplier);
                            System.debug(LoggingLevel.INFO, '---------new ship price--------' + result.price);
                        }
                    }
                }
            }catch(Exception e){
                   resultList.add(buildDefaultShippingOption(countryCode));                  
                }
 
                System.debug(LoggingLevel.INFO, 'Unfiltered result list = ' + resultList);
                List<ccrz.cc_ctrl_hlpr_ShippingOption> returnList = new List<ccrz.cc_ctrl_hlpr_ShippingOption>();  
                
                returnList.addAll(resultList);  
                                
                return sortBasedOnPrice(returnList);
                //return returnList;
            }catch (Exception e)
            {                
                //TODO jll, use correct error codes once custom settings are loaded
                cc_avid_util_ErrorHandling.logError('Error 101', 'Shipping Error: ' + e.getMessage() + 'CountryCode, Region' +countryCode +',' +avidRegion);
                System.debug(LoggingLevel.Info, e);
                if(resultList.size()==0){               
                               resultList.add(buildDefaultShippingOption(countryCode));                           
                }
                return resultList;
                //return null;
            }
        }
    }
   
    //TODO jll make these configurable
    public ccrz.cc_ctrl_hlpr_ShippingOption buildDefaultShippingOption(String countryCode){
        System.debug(LoggingLevel.INFO, '::::::::::::::::countryCode ' + countryCode);
        ccrz.cc_ctrl_hlpr_ShippingOption returnOption = new ccrz.cc_ctrl_hlpr_ShippingOption('FEDEX', 'FEDEX_GROUND', 15.00);// = new ccrz.cc_ctrl_hlpr_ShippingOption
        if('us'.equalsIgnoreCase(countryCode)){
            returnOption = new ccrz.cc_ctrl_hlpr_ShippingOption('FEDEX', 'FEDEX_GROUND', 15.00);
        }
        else if('de'.equalsIgnoreCase(countryCode)){
               returnOption = new ccrz.cc_ctrl_hlpr_ShippingOption('FEDEX', 'PRIORITY_OVERNIGHT', 15.00);
        }
        else
               returnOption = new ccrz.cc_ctrl_hlpr_ShippingOption('FEDEX', 'INTERNATIONAL_ECONOMY', 15.00);
 
        returnOption.currencyCode = UserInfo.getDefaultCurrency();
        return returnOption;
    }
}

Here i'm receving error on this line "global List<ccrz.cc_ctrl_hlpr_ShippingOption> fetchShippingOptions(String zipCode, String stateCode, String countryCode, Id cartId, String storeName) {" 

Please help..!!

NagendraNagendra (Salesforce Developers) 
Hi Surbhi,

You are missing a curly brace to close out this method:
global override List<ccrz.cc_ctrl_hlpr_ShippingOption> getShippingOptions

Add a brace at the end of this method, and you should be able to get rid of this error.

If you ever get other strange errors like this, try carefully formatting your code using an automated formatter (Sublime Text 3 has the AsStyleFormatter and there are a number of online options, like this one).

Hope this helps.

Please mark this as solved if it,s resolved.

Thanks,
Nagendra.