1

Although I am having this field in my custom settings it shows Error : Variable does not exist. Please find below code snippet. Can someone let me know what could be the reason?

Code Snippet:

FieldInsightsTrackerSettings_ABV__c userSet;
        if(LoginUserFranchiseName!=null)
        userSet = FieldInsightsTrackerSettings_ABV__c.getInstance(LoginUserFranchiseName);

    FieldInsightsTrackerSettings_ABV__c  userSet ; //= FieldInsightsTrackerSettings_ABV__c.getInstance(LoginUserFranchiseName);*/
    if(userSet!=null)
    { Franchise = userSet.Use_Franchise_abv__c;
      if(!Franchise )
      {FieldInsightsTrackerSettings_ABV__c  userSettings = FieldInsightsTrackerSettings_ABV__c.getInstance(LoginUserProfileId);}
      else { FieldInsightsTrackerSettings_ABV__c  userSettings= FieldInsightsTrackerSettings_ABV__c.getInstance(LoginUserFranchiseName);}
    } 

System.debug('----->'+userSettings); // This line throws error

2
  • Is second userSet variable declaration a typo? right under userSet = FieldInsightsTrackerSettings_ABV__c.getInstance(LoginUserFranchiseName);
    – user39399
    Jun 4, 2015 at 16:40
  • There's no System.debug call in the code snippet, is there?..
    – user39399
    Jun 4, 2015 at 16:50

2 Answers 2

3

Using consistent code indenting makes code like this and problems like this clearer. Indenting your code results in:

FieldInsightsTrackerSettings_ABV__c userSet;
if(LoginUserFranchiseName!=null) {
    userSet = FieldInsightsTrackerSettings_ABV__c.getInstance(LoginUserFranchiseName);
}

if(userSet!=null) {
    Franchise = userSet.Use_Franchise_abv__c;
    if(!Franchise) {
        FieldInsightsTrackerSettings_ABV__c  userSettings = FieldInsightsTrackerSettings_ABV__c.getInstance(LoginUserProfileId);
    } else {
        FieldInsightsTrackerSettings_ABV__c  userSettings= FieldInsightsTrackerSettings_ABV__c.getInstance(LoginUserFranchiseName);
    }
} 

System.debug('----->'+userSettings); // This line throws error

In Apex, a local variable's scope (whether it can be referenced) is limited to the code block it is in and multi-line code blocks start with { and end with }. So your userSettings variables are not visible where you have added your debug output.

This should avoid the error:

if(userSet!=null) {
    Franchise = userSet.Use_Franchise_abv__c;
    FieldInsightsTrackerSettings_ABV__c userSettings;
    if(!Franchise) {
        userSettings = FieldInsightsTrackerSettings_ABV__c.getInstance(LoginUserProfileId);
    } else {
        userSettings = FieldInsightsTrackerSettings_ABV__c.getInstance(LoginUserFranchiseName);
    }
    System.debug('----->'+userSettings);
} 
2
  • Hi Keith tried with your solution. It compiled well but page not loading properly. Jun 4, 2015 at 19:02
  • @SFDC_GEEK Usually to get code working you have to first fix compile errors and then logical errors.
    – Keith C
    Jun 4, 2015 at 20:20
2
if(LoginUserFranchiseName!=null)
FieldInsightsTrackerSettings_ABV__c userSet = FieldInsightsTrackerSettings_ABV__c.getInstance(LoginUserFranchiseName);

What if LoginUserFranchiseName is not null? Then you don't declare userSet at all, do you? So if it is not null, the next if statement is not aware of the variable you are evaluating.

if(userSet!=null)

To me it should be:

FieldInsightsTrackerSettings_ABV__c userSet;

if(LoginUserFranchiseName!=null)
 userSet = FieldInsightsTrackerSettings_ABV__c.getInstance(LoginUserFranchiseName);

I'm assuming here that Franchise variable is declared somewhere outside the given code.

You must log in to answer this question.

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