0

For the following class, I get the error 'variable does not exist: mq' on lines 25 - 27

Public with sharing class InspectionQuotasHandler{

    public static void UpdateMonthlyQuotaOfInspection(List<Inspection__c> InspectionQuotasList){

        Set<id> AccountId = new Set<Id>();

        for (Inspection__c i : (List<Inspection__c>)Trigger.new) {
            AccountId.add(i.Franchise__c);
        }

        Map<String,map<String,Id>> keyByLookupMap = new Map<String,map<String,Id>>();

        for(Monthly_Quota__c mq : [SELECT Id, Name,Account__c 
                           FROM Monthly_Quota__c 
                           WHERE Account__c = :AccountId]){

            if(!keyByLookupMap.containsKey(mq.Account__c))
                keyByLookupMap.put(mq.Account__c, new map<String,Id>());
            keyByLookupMap.get(mq.Account__c).put(mq.Name,mq.Id);

        }

        if(!keyByLookupMap.isEmpty()){
            for(Inspection__c i : (List<Inspection__c>)trigger.new){
                if(i.Inspection_Date__c != null && keyByLookupMap.containsKey(mq.Account__c) && keyByLookupMap.get(mq.Account__c).containsKey(convertMonthToWord(i.Inspection_Date__c.month()))){
                    if(i.Status__c == 'Completed' && i.Month_Since_Launch__c > 0){
                        i.Monthly_Quota__c = keyByLookupMap.get(mq.Account__c).get(convertMonthToWord(i.Inspection_Date__c.month()));
                    }
                }            
            }
        }
    }

    public static String convertMonthToWord(Integer monthIndex){

        if(monthIndex==1){
            return 'January';
        }else if(monthIndex==2){
            return 'February';
        }else if(monthIndex==3){
            return 'March';
        }else if(monthIndex==4){
            return 'April';
        }else if(monthIndex==5){
            return 'May';
        }else if(monthIndex==6){
            return 'June';
        }else if(monthIndex==7){
            return 'July';
        }else if(monthIndex==8){
            return 'August';
        }else if(monthIndex==9){
            return 'September';
        }else if(monthIndex==10){
            return 'October';
        }else if(monthIndex==11){
            return 'November';
        }else if(monthIndex==12){
            return 'December';
        }else{
            return null;
        }
    }
}

How do I fix this?

2
  • Being consistent with indentation can help identify issues, and always being explicit with curly braces for if/else statements (even if it's only one line inside the 'if' or 'else' blocks) can help avoid errors. In this instance, if you think that if(!keyByLookupMap.isEmpty()){ on line 23 should be inside your for(Monthly_Quota__c mq : [SELECT Id, Name,Account__c ... on line 13 (which would keep the variable mq in-scope), then proper indentation would reveal that you're missing an opening curly brace on line 17.
    – Derek F
    Commented Feb 22, 2018 at 20:56
  • As is, I've formatted your code to reflect what Salesforce thinks your code is (without explicit opening/closing braces on if/else, the if/else only applies to the line directly following the if/else).
    – Derek F
    Commented Feb 22, 2018 at 20:57

3 Answers 3

2

The mq variable is scoped to the loop over your Monthly_Quota__c query. It's not defined once that loop completes, which it does just above the lines where you see this error. Did you mean to use i rather than mq there?

5
  • Thanks. What I need is for the lines 25 - 27 to pull in the values from the keybylookupmap after the values have been added via the for loop
    – Sebastian
    Commented Feb 22, 2018 at 20:28
  • Yes, but for which Account? The account for the Inspection__c you're looking at?
    – David Reed
    Commented Feb 22, 2018 at 20:36
  • Yes, for the Inspection_c account.
    – Sebastian
    Commented Feb 22, 2018 at 20:43
  • Then you need to be accessing an instance variable on i to get its Account Id.
    – David Reed
    Commented Feb 22, 2018 at 20:46
  • Thanks for helping out this newbie. I got it to work with your input.
    – Sebastian
    Commented Feb 22, 2018 at 21:01
1

Line 21 closes the loop, so you can't see "mq" beyond this line. If mq was meant to be used, it's likely you meant to place it later, perhaps around line 32. In the future, make sure you're matching up your indentation, because it would have been "obvious" why this was happening. Also, with regards to the if statement that doesn't have curly braces around its body; never, ever do this. You're just setting yourself up for failure later when someone doesn't realize what's going on or simply misreads the code.

Public with sharing class InspectionQuotasHandler{

    public static void UpdateMonthlyQuotaOfInspection(List<Inspection__c> InspectionQuotasList){

        Set<id> AccountId = new Set<Id>();

        for (Inspection__c i : (List<Inspection__c>)Trigger.new) {
            AccountId.add(i.Franchise__c);
        }

        Map<String,map<String,Id>> keyByLookupMap = new Map<String,map<String,Id>>();

        for(Monthly_Quota__c mq : [SELECT Id, Name,Account__c 
                           FROM Monthly_Quota__c 
                           WHERE Account__c = :AccountId]){

            if(!keyByLookupMap.containsKey(mq.Account__c)) {
                keyByLookupMap.put(mq.Account__c, new map<String,Id>());
            }
            keyByLookupMap.get(mq.Account__c).put(mq.Name,mq.Id);

        }

        if(!keyByLookupMap.isEmpty()){
            for(Inspection__c i : (List<Inspection__c>)trigger.new){
                if(i.Inspection_Date__c != null && keyByLookupMap.containsKey(mq.Account__c) && keyByLookupMap.get(mq.Account__c).containsKey(convertMonthToWord(i.Inspection_Date__c.month()))){
                    if(i.Status__c == 'Completed' && i.Month_Since_Launch__c > 0){
                        i.Monthly_Quota__c = keyByLookupMap.get(mq.Account__c).get(convertMonthToWord(i.Inspection_Date__c.month()));
                    }
                }            
            }
        }
    }
........
0

Thanks everyone for helping out the neophyte. This works perfectly:

Public with sharing class InspectionQuotasHandler{

    public static void UpdateMonthlyQuotaOfInspection(List<Inspection__c> InspectionQuotasList){

        Set<id> AccountId = new Set<Id>();

        for (Inspection__c i : (List<Inspection__c>)Trigger.new) {
            AccountId.add(i.Franchise__c);
        }

        Map<String,map<String,Id>> keyByLookupMap = new Map<String,map<String,Id>>();

        for(Monthly_Quota__c mq : [SELECT Id, Name,Account__c 
                           FROM Monthly_Quota__c 
                           WHERE Account__c = :AccountId]){

            if(!keyByLookupMap.containsKey(mq.Account__c)) {
                keyByLookupMap.put(mq.Account__c, new map<String,Id>());
            }
            keyByLookupMap.get(mq.Account__c).put(mq.Name,mq.Id);

        }

        if(!keyByLookupMap.isEmpty()){
            for(Inspection__c i : (List<Inspection__c>)trigger.new){
                if(i.Inspection_Date__c != null && keyByLookupMap.containsKey(i.Franchise__c) && keyByLookupMap.get(i.Franchise__c).containsKey(convertMonthToWord(i.Inspection_Date__c.month()))){
                    if(i.Status__c == 'Completed' && i.Month_Since_Launch__c > 0){
                        i.Monthly_Quota__c = keyByLookupMap.get(i.Franchise__c).get(convertMonthToWord(i.Inspection_Date__c.month()));
                    }
                }            
            }
        }
    }

    public static String convertMonthToWord(Integer monthIndex){

        if(monthIndex==1){
            return 'January';
        }else if(monthIndex==2){
            return 'February';
        }else if(monthIndex==3){
            return 'March';
        }else if(monthIndex==4){
            return 'April';
        }else if(monthIndex==5){
            return 'May';
        }else if(monthIndex==6){
            return 'June';
        }else if(monthIndex==7){
            return 'July';
        }else if(monthIndex==8){
            return 'August';
        }else if(monthIndex==9){
            return 'September';
        }else if(monthIndex==10){
            return 'October';
        }else if(monthIndex==11){
            return 'November';
        }else if(monthIndex==12){
            return 'December';
        }else{
            return null;
        }
    }
}

You must log in to answer this question.

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