+ Start a Discussion
moverdorfmoverdorf 

Custom Settings Question

I have the following custom setting: Payout__AH_FromToDate__c
 
I created two values in the Custom Settings panel:
payout__AH_From_Date__c
payout__AH_To_Date__c
 
 
In my APEX Class code:
 
237        Date frmdte;
 238       Date todte;
 
   239     frmdte = payout__AH_FromToDate__c.getValues('payout__AH_From_Date__c');
    240    todte  = payout__AH_FromToDate__c.getValues('payout__AH_To_Date__c');
 
    241    DateInput1.ActivityDate  = frmdte;
     242   DateInput2.ActivityDate  = todte;
 
Error: Compile Error: Illegal assignment from SOBJECT:payout__AH_FromToDate__c to Date at line 239 column 9
 
Can someone help me out as to what I need to do so that I can assign frmdte and todte to my custom setting date values?
 
Thanks.
 
 
Best Answer chosen by moverdorf
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

It seems that Apex Code doesn't gets the default values calculated. If it wasn't a formula you could get field's default value like this:
 

payout__AH_From_Date__c.YourField__c.getDescribe().getDefaultValue();

In your case it is a formula so I don't see how you can get the default value. However you can add an if/else condition to reproduce what the default values were supposed to do:
 
if(!lstSettng.isEmpty()){
	if (lstSettng[0].payout__AH_From_Date__c != null)
		frmdte = lstSettng[0].payout__AH_From_Date__c;
	else
		frmdte = System.today() - 10;
	
	if (lstSettng[0].payout__AH_To_Date__c != null)
		todte  = lstSettng[0].payout__AH_To_Date__c;
	else
		frmdte = System.today() + 10;
}
Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
 

All Answers

Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Can you share your entire code so we can have a look? You can disguise names if needed.

Regards.
Bhanu MaheshBhanu Mahesh
Hi,

Try the below code
 
 Date frmdte;
 Date todte;
List<payout__AH_FromToDate__c> lstSettng = payout__AH_FromToDate__c.getAll().Values();
if(!lstSettng.isEmpty()){
     frmdte = lstSettng[0].payout__AH_From_Date__c;
      todte  = lstSettng[0].payout__AH_To_Date__c;  
}

 DateInput1.ActivityDate  = frmdte;
 DateInput2.ActivityDate  = todte;


Regards,
Bhanu Mahesh
Ankit Maini.Ankit Maini.
hi moverdorf,

I assume that your custom settings is of type LIST:

One way is to get Values is in list:
 
List<payout__AH_FromToDate__c> lstSettng  = payout__AH_FromToDate__c.getall().values();

Another way is:
payout__AH_FromToDate__c lstSettng   = payout__AH_FromToDate__c.getValues('This is the value of custom settins');
Date frmdte = lstSettng.payout__AH_From_Date__c;


 
moverdorfmoverdorf
Thanks everyone,

I tried the following:
Date frmdte;
 Date todte;
List<payout__AH_FromToDate__c> lstSettng = payout__AH_FromToDate__c.getAll().Values();
if(!lstSettng.isEmpty()){
     frmdte = lstSettng[0].payout__AH_From_Date__c;
      todte  = lstSettng[0].payout__AH_To_Date__c;  
}

 DateInput1.ActivityDate  = frmdte;
 DateInput2.ActivityDate  = todte;

And lstSettng is empty (null) for some reason... I assumed that when I defined the values for the custom setting I made the default value Date()-10 and Date()+10. Does setting the default value automatically mean that lstSettng should now have values in the list when the object is created? Or do I need to populate it another way?

Thanks guys.
 
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

It seems that Apex Code doesn't gets the default values calculated. If it wasn't a formula you could get field's default value like this:
 

payout__AH_From_Date__c.YourField__c.getDescribe().getDefaultValue();

In your case it is a formula so I don't see how you can get the default value. However you can add an if/else condition to reproduce what the default values were supposed to do:
 
if(!lstSettng.isEmpty()){
	if (lstSettng[0].payout__AH_From_Date__c != null)
		frmdte = lstSettng[0].payout__AH_From_Date__c;
	else
		frmdte = System.today() - 10;
	
	if (lstSettng[0].payout__AH_To_Date__c != null)
		todte  = lstSettng[0].payout__AH_To_Date__c;
	else
		frmdte = System.today() + 10;
}
Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
 
This was selected as the best answer
moverdorfmoverdorf
Thanks, I need to be able to put in custom From/To dates for each client. I was going to do it using the Today()+7 or +9 or whatever the client requested.

So it sounds like instead of using the formula to create the value, I need to hard code a value value like 7 or 9 and then use that value in the Apex
code where I can then use the System.today() + my custom value.

Appreciate the help.
Thanks.

 
moverdorfmoverdorf
Still not getting it...

I created instead of dates just Number fields:
1. To_Days (Number)
2.From_Days (Number)

Now 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?

Thanks.