5

When I try to save my apex class with the following method in it, it is giving me an error "Error: Compile Error: Variable does not exist: Helper.prod_field1_api_name__c at line 37 column 41" (line 37 in my class is the highlighted line below)

global List<Related_Prods_Firms__c> start(Database.BatchableContext BC) 
    {
        try
        {
            Helper__c Helper = [Select firm_api_name__c,firm_field_api_name__c,
                                        Org_ID__c,Username__c,Password__c,prod_api_name__c,
                                        prod_field1_api_name__c,prod_field2_api_name__c
                                        from Helper__c];
        }
        catch(System.QueryException q)
        {
            String errMsg = q.getMessage();
            if(errMsg.contains('List has no rows for assignment to SObject'))
            {
                //add logic to be executed when there is no Helper__c record
            }
            if(errMsg.contains('List has more than 1 row for assignment to SObject'))
            {
                //add logic to be executed when there is more than one Helper__c record
            }
            return null;
        }
        List<Related_Prods_Firms__c> currRelatedProdFirmsList = new List<Related_Prods_Firms__c>();
        String query = 'Select Firm__c,Prod__c';
        if(Helper!=null)
        {
            if(Helper.prod_field1_api_name__c!=null&&Helper.prod_field1_api_name__c!='')
            {
                **query += ','+'Prod__r.'+Helper.prod_field1_api_name__c;**
            }
            if(Helper.prod_field2_api_name__c!=null&&Helper.prod_field2_api_name__c!='')
            {
                query += ','+'Prod__r.'+Helper.prod_field2_api_name__c;
            }
            if(Helper.firm_field_api_name__c!=null&&Helper.firm_field_api_name__c!='')
            {
                query += ','+'Firm__r.'+Helper.firm_field_api_name__c;
            }
        }
        query += ' from Related_Prods_Firms__c';
        if(Helper!=null)
        {
            query = String.escapeSingleQuotes(query);
            currRelatedProdFirmsList = Database.query(query);
        }
        return currRelatedProdFirmsList;
    }

If I repeat the "Helper" query part (as below), then it is saving the class!

global List<Related_Prods_Firms__c> start(Database.BatchableContext BC) 
    {
        try
        {
            **Helper__c Helper = [Select firm_api_name__c,firm_field_api_name__c,
                                        Org_ID__c,Username__c,Password__c,prod_api_name__c,
                                        prod_field1_api_name__c,prod_field2_api_name__c
                                        from Helper__c];**
        }
        catch(System.QueryException q)
        {
            String errMsg = q.getMessage();
            if(errMsg.contains('List has no rows for assignment to SObject'))
            {
                //add logic to be executed when there is no Helper__c record
            }
            if(errMsg.contains('List has more than 1 row for assignment to SObject'))
            {
                //add logic to be executed when there is more than one Helper__c record
            }
            return null;
        }
        **Helper__c Helper = [Select firm_api_name__c,firm_field_api_name__c,
                                        Org_ID__c,Username__c,Password__c,prod_api_name__c,
                                        prod_field1_api_name__c,prod_field2_api_name__c
                                        from Helper__c];**
        List<Related_Prods_Firms__c> currRelatedProdFirmsList = new List<Related_Prods_Firms__c>();
        String query = 'Select Firm__c,Prod__c';
        if(Helper!=null)
        {
            if(Helper.prod_field1_api_name__c!=null&&Helper.prod_field1_api_name__c!='')
            {
                query += ','+'Prod__r.'+Helper.prod_field1_api_name__c;
            }
            if(Helper.prod_field2_api_name__c!=null&&Helper.prod_field2_api_name__c!='')
            {
                query += ','+'Prod__r.'+Helper.prod_field2_api_name__c;
            }
            if(Helper.firm_field_api_name__c!=null&&Helper.firm_field_api_name__c!='')
            {
                query += ','+'Firm__r.'+Helper.firm_field_api_name__c;
            }
        }
        query += ' from Related_Prods_Firms__c';
        if(Helper!=null)
        {
            query = String.escapeSingleQuotes(query);
            currRelatedProdFirmsList = Database.query(query);
        }
        return currRelatedProdFirmsList;
    }

I don't understand why I have to define the "Helper" part twice! Can anyone please explain me this? Thanks in advance.

11

Your helper is getting scoped inside of the try block. If you instantiate it outside of the try block, then it will persist in a larger scope.

E.g. Where Helper is out of scope

try
{
    // Helper will only be in scope within this try block. 
    // You won't be able to reference it in code beyond the closing brace.
    Helper__c Helper = [Select firm_api_name__c from Helper__c];
}
catch(System.QueryException q)
{
    // You can't access Helper here
    String errMsg = q.getMessage();
    return null;
}
// You can't access Helper here

E.g. Where Helper has been scoped to the method

global List<Related_Prods_Firms__c> start(Database.BatchableContext BC)
{
    // Helper is now scoped within the method and can be used until the method is closed.
    // Note that is could be Null though, such as in the catch block. 
    Helper__c Helper = null;

    try
    {
        Helper = [Select firm_api_name__c from Helper__c];
    }
    catch(System.QueryException q)
    {
        // You can access Helper here, but it will be null.
        String errMsg = q.getMessage();
        return null;
    }
    // You can access Helper here (check for nulls depending on the exception handling above)
}
0

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.