+ Start a Discussion
moverdorfmoverdorf 

How To Get The Values Of Custom Settings

I created the following custom field payout__ AH_DAY_RANGES__c with TWO custom setting values:
1. To_Days (Number)
2.From_Days (Number)

Now in my apex class, I simply want to set my date input fields to the current date +/- the custome values...

If(payout__ AH_DAY_RANGES__c.From_Days__c.getDescribe().getDefaultValue() != null)
{
            DateInput1.ActivityDate = System.Today() - payout__ AH_DAY_RANGES__c.From_Days__c.getDescribe().getDefaultValue();
}
 
 
If(payout__ AH_DAY_RANGES__c.To_Days__c.getDescribe().getDefaultValue() != null)
{
            DateInput2.ActivityDate = System.Today() + payout__ AH_DAY_RANGES__c.To_Days__c.getDescribe().getDefaultValue();
}

But I am getting an error because the data type of payout__ AH_DAY_RANGES__c.To_Days__c.getDescribe().getDefaultValue() is OBJECT and not what I expected it to be (Number). How can I make the datatype of payout__ AH_DAY_RANGES__c.To_Days__c.getDescribe().getDefaultValue() a NUMBER so I can use it in the System.Today() formula?
James LoghryJames Loghry

There are two types of custom settings (and thus two different methods) for retreiving values via custom settings.  It appears your custom setting is a Hierarchy setting judging by your example.  Here's an example of how you would fetch the field from a Hierarchy Setting:

DateInput1.ActivityDate = System.Today() - payout__AH_DAY_RANGES.getInstance().From_Days__c;
 


If it was a list setting, you would use the following:
 

DateInput1.ActivityDate = System.Today() - payout__AH_DAY_RANGES.getValues('someKey').From_Days__c;
For more on custom setting methods see this page: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_custom_settings.htm
moverdorfmoverdorf
My code is blowing up, because my default value I am setting in my Custom Setting field (10) is not being found when I try and retireve it in my APEX code.
 
 
In my APEX code I have:
 
List<payout__AH_DAY_RANGES__c> lstSettng = payout__AH_DAY_RANGES__c.getAll().Values();
 
System.debug(lstSettng);
 
Integer integerValue1 = lstSettng[0].payout__From_Days__c.intValue();
Integer integerValue2 = lstSettng[0].payout__To_Days__c.intValue();

 
integerValue1 is null, why isn’t it 10 which is what I have entered as my default value?
 
 
Here is the DEBUG info, what I don’t see is the DEFAULT value of 10

11:59:49.133 (133949731)|USER_DEBUG|[240]|DEBUG|(payout__AH_DAY_RANGES__c:{Name=To_Days, SetupOwnerId=00DA0000000YpalMAC, LastModifiedById=005A0000000JrNJIA0, SystemModstamp=2015-02-06 23:48:48, CreatedById=005A0000000JrNJIA0, CreatedDate=2015-02-06 23:46:31, LastModifiedDate=2015-02-06 23:48:48, IsDeleted=false, Id=a11A0000002vzuzIAA}, payout__AH_DAY_RANGES__c:{Name=From_Days, SetupOwnerId=00DA0000000YpalMAC, LastModifiedById=005A0000000JrNJIA0, SystemModstamp=2015-02-06 23:49:07, CreatedById=005A0000000JrNJIA0, CreatedDate=2015-02-06 23:46:17, LastModifiedDate=2015-02-06 23:49:07, IsDeleted=false, Id=a11A0000002vzuuIAA})
 
 
Do I need to do something else to use 10 as my custom value?... I thought by putting in 10 as me DEFAULT VALUE when defining the fields that was all I needed to do. So why is it NULL when I try and retrieve it?
 
 
 
 
 
Below is how I setup the custom fields:
Custom Setting Definition
AH_DAY_RANGESHelp for this Page
Create the fields for your custom setting. The data in these fields are cached with the application.
Custom Setting Definition Detail

Label
AH_DAY_RANGES
Object Name
AH_DAY_RANGES
API Name
payout__AH_DAY_RANGES__c
Setting Type
List
Visibility
Public
Description
Activity History Date Ranges
Namespace Prefix
payout
Created Date
2/6/2015 6:44 PM
Last Modified Date
2/6/2015 6:45 PM
Record Size
136





Custom Fields

 
 
Action
 
Field Label
API Name
Data Type
Indexed
 
Modified By
Edit | Del
 
From_Days
payout__From_Days__c
Number(18, 0)
 
 
Michael Overdorf, 2/6/2015 7:04 PM
Edit | Del
 
To_Days
payout__To_Days__c
Number(18, 0)
 
 
Michael Overdorf, 2/6/2015 7:04 PM
 
 
 
 
 
 
 
 
AH_DAY_RANGES Custom Field
From_Days 
Help for this Page
  Back to AH_DAY_RANGES
 



Custom Field Definition Detail

Field Information

Field Label
From_Days
Object Name
AH_DAY_RANGES
Field Name
From_Days
Data Type
Number
Namespace Prefix
payout
 
 
API Name
payout__From_Days__c
 
 
Description
Help Text
Created By
Michael Overdorf, 2/6/2015 7:04 PM
Modified By
Michael Overdorf, 2/6/2015 7:04 PM


General Options

Required
[Not Checked]
Unique
[Not Checked]
External ID
[Not Checked]
Default Value
10

 
 
 
Akshay MunjalAkshay Munjal
@Moverdof

As you are using the heirarchy custom setting and you have set the default value in formula so below code will work perfectly for you.

payout__ AH_DAY_RANGES__c.From_Days__c.getDescribe().getDefaultValueFormula()