2

I have this variable which returns key from custom settings. below is the code related to this

  private static String AccLinks {
    get {

        if (Acclinks == null) {
            Key_Value_List__c kvl = Key_Value_List__c.getInstance('Acc_Content');
            AccLinks = (kvl != null && kvl.Value__c != null ? kvl.Value__c : 'some_url');
        }
        return AccLinks;
    }
}

when I run this code, I am getting this error

Variable is not visible: Acc_Class2.Acclinks

Can anyone help me with this

6

You cannot assign to a property

AccLinks = (kvl != null && kvl.Value__c != null ? kvl.Value__c : 'some_url');

if that property does not have a setter method declared. To make the property read-only while caching its value (a lazy-loading pattern), you can synthesize a private setter:

get {
    // ...
}
private set;

That will allow your class itself to set the value. See Apex Properties for more.

1

As mentioned, you cannot set the value to your property when you don't have a setter defined.

In your case, you can instead use the below code where you won't be able to set the value for your variable, but you will always get the value from your custom setting record.

private static String AccLinks { 
    get {
        string returnValue = 'some_url'; //you can keep any default value
        if (Acclinks == null) { 
            Key_Value_List__c kvl = Key_Value_List__c.getInstance('Acc_Content'); 
            returnValue = (kvl != null && kvl.Value__c != null ? kvl.Value__c : 'some_url'); 
        }
        return returnValue;   
    } 
}

Another option would be to have a private setter using which you can set the value to your variable

private static String AccLinks {
    get {
        if (Acclinks == null) {
            Key_Value_List__c kvl = Key_Value_List__c.getInstance('Acc_Content');
            AccLinks = (kvl != null && kvl.Value__c != null ? kvl.Value__c : 'some_url');
        }
        return AccLinks;
    } private set;
}

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.