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
Julian Davis 7Julian Davis 7 

cant get beyond an "error: Compile Error: Variable does not exist" error

Hello

I'm possibly a bit too new to coding in Salesforce, so please bear with the newbie, but I am getting an error when trying to create an Apex class that I am really struggling to get beyond, though its probably super-simple (hopefully!).

Here is the code for my class...
 
public class dynamicaddresslist {
    
public List<address__c> addressTemp = new List<Address>();
    public dynamicaddresslist()
    {
     
    }
     
    public List<SelectOption> addresslist
    {
        get
        {
            addressTemp = [Select Address_Type__c,City__c,Country__c,Postal_Code__c,State__c,Street__c from address__c];
            
            addresslist = new List<SelectOption>();
             
            for(address adr : addressTemp)
            {
                addresslist.add(new SelectOption(adr.AddressType, adr.Street__c, adr.City__c, adr.State__c, adr.Postal_Code__c, adr.Country__c));
            }
            return addresslist;
        }
        set;
    }
}

the object that I am hoping to pull the records from is a custom one, titled "Address__c"

Any points in the right direction of where I am going wrong would be much appreciated.

Thank you in advance for your help.

Julian
Best Answer chosen by Julian Davis 7
Maharajan CMaharajan C
Hi Julian,

Please try the below changes:

public class dynamicaddresslist {
    
public List<address__c> addressTemp = new List<Address>();
    public dynamicaddresslist()
    {
     
    }
     
    public List<SelectOption> addresslist
    {
        get
        {
            addressTemp = [Select Address_Type__c,City__c,Country__c,Postal_Code__c,State__c,Street__c from address__c];
            
            List<SelectOption> addresslist = new List<SelectOption>();   // This Line needs be changed
             
            for(address adr : addressTemp)
            {
                // The below commmented line is wrong.
                //the select option will provide dropdown values to the Custom Dropdowns in VF Page. So it will contain only the Name and Value.
                // addresslist.add(new SelectOption(adr.AddressType, adr.Street__c, adr.City__c, adr.State__c, adr.Postal_Code__c, adr.Country__c));
                
                addresslist.add((new SelectOption(adr.State__c,adr.State__c));    // (Display Name , Value )

            return addresslist;
        }
        set;
    }
}

Please refer the link below:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_pages_selectoption.htm

Thanks,
Maharajan.C

All Answers

Arun MohanArun Mohan
Hi Julian,

Below is the code with no error,
 
public class dynamicaddresslist{
public List<address__c> addressTemp = new List<Address__c>();
    public dynamicaddresslist()
    {
     
    }
     
    public List<SelectOption> addresslist
    {
        get
        {
            addressTemp = [Select Address_Type__c,City__c from address__c];
            
            addresslist = new List<SelectOption>();
             
            for(address__c adr : addressTemp)
            {
                addresslist.add(new SelectOption(adr.Address_Type__c,adr.City__c));
            }
            return addresslist;
        }
        set;
    }
}
Please let me know, if you are still facing issue.
​​​​​​​
Thanks
Arun
SUCHARITA MONDALSUCHARITA MONDAL

Hi Julian,

Can you post error in detail ( may be logs of error).

Apart from this, there are some points, I'm adding up for you.

LINE -03    public List<address__c> addressTemp = new List<Address>();

It'll be public List<address__c> addressTemp = new List<address__c>();

LINE-19   addresslist.add(new SelectOption(adr.AddressType, adr.Street__c, adr.City__c, adr.State__c, adr.Postal_Code__c, adr.Country__c)); 

AddressType SHOULD BE 'Address_Type__c'  -  (Api name is needed NOT LABEL,).

Secondly, whenever you query, put limit or criteria to fetch limited records (otherwise you'll hit governor limits).

Query is performed using API Names.

Hope this helps!

Sucharita

Maharajan CMaharajan C
Hi Julian,

Please try the below changes:

public class dynamicaddresslist {
    
public List<address__c> addressTemp = new List<Address>();
    public dynamicaddresslist()
    {
     
    }
     
    public List<SelectOption> addresslist
    {
        get
        {
            addressTemp = [Select Address_Type__c,City__c,Country__c,Postal_Code__c,State__c,Street__c from address__c];
            
            List<SelectOption> addresslist = new List<SelectOption>();   // This Line needs be changed
             
            for(address adr : addressTemp)
            {
                // The below commmented line is wrong.
                //the select option will provide dropdown values to the Custom Dropdowns in VF Page. So it will contain only the Name and Value.
                // addresslist.add(new SelectOption(adr.AddressType, adr.Street__c, adr.City__c, adr.State__c, adr.Postal_Code__c, adr.Country__c));
                
                addresslist.add((new SelectOption(adr.State__c,adr.State__c));    // (Display Name , Value )

            return addresslist;
        }
        set;
    }
}

Please refer the link below:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_pages_selectoption.htm

Thanks,
Maharajan.C
This was selected as the best answer