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
krishna casukhela 7krishna casukhela 7 

unable to access custom setting in apex

Hi
I have  created a custom setting whose api name is SonyMusicUK__c and added value to it as       Sony Music UK
Now in my soql I want to refer to this custom setting but I am unable to do so

SonyMusicUK__c p;  //create an object for custom setting
p=[select Name from SonyMusicUK__c];
system.debug(p);
List<subscription__c> lstsub=new List<subscription__c>();
lstsub=[select s.Mailing_List_Name__c
                               from subscription__c s
                               where s.Territory_ID__c.Territory__c LIKE (p)];
system.debug(lstsub);
system.debug(lstsub.size());

I am not getting desired result, any better way of doing this.

thanks
krishna
Best Answer chosen by krishna casukhela 7
jigarshahjigarshah
Krishna,

Your code now becomes as below.

Apex Controller Method
private List<SelectOption> countryCodeList = new List<SelectOption>();

//Holds the Country Code for the selected option
public String selectedCountryCode {get; set;}

//Property to hold the Country Codes
public List<SelectOption> getCountryCodes(){
	if(countryCodeList.isEmpty()){
		countryCodeList.add(new SelectOption("None", "--Select--"));
		for(Countries__c country :Countries__c.getAll().values()){
			countryCodeList.add(new SelectOption(country.CountryCode__c, country.Name));
		}
	}
	return countryCodeList;
}
Visualforce Code
<apex:selectList size="1" value="{!selectedCountryCode}">
  <apex:selectOptions value="{!getCountryCodes}"/>
</apex:selectList>
Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it help you resolves your issue.

All Answers

krishna casukhela 7krishna casukhela 7
Hello
a small change in query.  
SonyMusicUK__c p;
p=[select Name from SonyMusicUK__c];
system.debug(p);
List<subscription__c> lstsub=new List<subscription__c>();
lstsub=[select s.Mailing_List_Name__c
                               from subscription__c s
                               where s.Territory__c : (p)];
system.debug(lstsub);
system.debug(lstsub.size());

Here Territory__c is a formula field in the object subscription__c

But when I execute I am getting error as Unexpectd token : near the where condition.

Thanks
krishna
 
jigarshahjigarshah
Krishna,

The advantage of using custom settings is that the stored data is cached, and is made available with available library functions, without the need of a SOQL query to retrieve it. Althought Salesforce permits querying a custom setting, it is not recommended.

Your code now becomes
//Set to store the names of the retrieved territories
Set<String> sonyMusicSettingNameSet = new Set<String>();

//Assuming that the Territory Name is stored in the Name field of SonyMusicUK__c
for(SonyMusicUK__c sonyMusicSetting :SonyMusicUK__c.getAll().values()){
	sonyMusicSettingNameSet.add(sonyMusicSetting.Name);
}

List<subscription__c> lstsub = [select s.Mailing_List_Name__c
								from subscription__c s
								where s.Territory_ID__c.Territory__c IN :sonyMusicSettingNameSet];
system.debug(lstsub);
system.debug(lstsub.size());

Refer the following link to understand Custom Settings better - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm

Hope this helps.

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it help you resolves your issue.
krishna casukhela 7krishna casukhela 7
HI
I got the desired result. thanks

I want to clarify one more point before I close this post.

I have created a new custom setting say Countries (api name : Countries__c) and has field CountryCode__c
this means
 Afghanisthan   AK
 Australia          AU   
New Zealand    NZ

Now I have the visualforce page as follows.
<apex:selectList value="{!fanCountry_Region}" size="1">
   <apex:selectOption itemValue="None" itemLabel="--Select--"></apex:selectOption> <
     apex:selectOption itemValue="United Kingdom" itemLabel="United Kingdom">
  </apex:selectOption> <apex:selectOption itemValue="India" itemLabel="Germany">
 </apex:selectOption> <apex:selectOption itemValue="SriLanka" itemLabel="Australia">
 </apex:selectOption> <apex:selectOption itemValue="United States of America" itemLabel="United States of America">
</apex:selectOption>
</apex:selectList>
so I need to remove the above values and need to fetch the values from the custom setting .
Pls let me know what changes should I make to visualforce page and how to fetch values in controller for picklist.

thanks
krishna
                
jigarshahjigarshah
Krishna,

Your code now becomes as below.

Apex Controller Method
private List<SelectOption> countryCodeList = new List<SelectOption>();

//Holds the Country Code for the selected option
public String selectedCountryCode {get; set;}

//Property to hold the Country Codes
public List<SelectOption> getCountryCodes(){
	if(countryCodeList.isEmpty()){
		countryCodeList.add(new SelectOption("None", "--Select--"));
		for(Countries__c country :Countries__c.getAll().values()){
			countryCodeList.add(new SelectOption(country.CountryCode__c, country.Name));
		}
	}
	return countryCodeList;
}
Visualforce Code
<apex:selectList size="1" value="{!selectedCountryCode}">
  <apex:selectOptions value="{!getCountryCodes}"/>
</apex:selectList>
Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it help you resolves your issue.
This was selected as the best answer
krishna casukhela 7krishna casukhela 7
Hi
I able to populate the list of countries in picklist when the page loads.
Here I have a issue.
I have custom object fan__c which has say two fields country_region__c and email.
In browser URL I will append the encryptedID of the fan object and fetch the values into their respective fields in visual force page
assume in record we have   country_region__c=India and email=abc@yahoo.co.in

so these values will appear in page but country is not coming in picklist.

Here is my code.
public class Pref6 {

    public String fanCountry_Region { get; set; }
    public String fanEmail { get; set; } 

    public String encryptedfanID{get;set;}
       
    public fan__c fan{get;set;}

   public Pref6()
    {
        encryptedfanID=ApexPages.currentpage().getparameters().get('id');
               
        if(String.isBlank(encryptedfanID))
        {
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Id.');
                ApexPages.addMessage(myMsg);
                return;
        }
        else
        {
          try
          {
            fetchfanvalues();
          }
          catch(System.QueryException ex){
                
              Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR, 'No Record'));
          }
}

 public void fetchfanvalues()
    {
         fan = new fan__c();
         
         fan=[SELECT id, Email__c,,Country_Region__c
             FROM fan__c WHERE Encrypted_ID__c=:encryptedfanID];
         
         if (fan != NULL)
         {
             if(!string.isBlank(fan.Email__c))
             {
                fan_email=fan.Email__c;
                fanEmail=maskEmail(fan.Email__c);
             }

       if(!string.isBlank(fan.Country_Region__c))
       {
          system.debug('country'+fan.Country_Region__c);
          fanCountry_Region=fan.Country_Region__c; 
         }
}

 <apex:selectList value="{!fanCountry_Region}" size="1">

                <apex:selectOptions value="{!countryCodes}"></apex:selectOptions>
                                                 
  </apex:selectList>

I am getting the correct value in fanCountry_Region but in visual force page it is not showing.

Thanks
krishna