1

I am having an requirement. I am having Number field (NumberField_c) in a custom Setting (MyCSObj_c).

So I have to retrieve NumberField__c value from custom seting and need to check whether that number has any fractional part or not.

If that number doesn't has any fractional part then subtract that number of months from the end date and use that value for start date.

public with sharing class MyClassController {

public DummySObject__c dummyObj{get; set;}

public MyClassController(){
   dummyObj = new DummySObject__c();
   dummyObj.EndDateA__c = system.today();

MyCSObj__c appConstantSettings = MyCSObj__c.getInstance();

if(appConstantSettings != null 
&& appConstantSettings .NumberField__c!= null){
   Decimal interaction_Month = appConstantSettings.NumberField__c;
}

}

My questions are : 1) How to check the number is fractional or not 2) How to subtract any number from current date? (For example: Today's date : 2014-03-18 00:00:00; Number we get 1. So we have to reduce 1 month from current date.

Any help'd be appriciated.

2
  • You might want to change the 2 lines checking for null values from MyCSObj__c!= null to appConstantSettings!= null and also from MyCSObj__c.NumberField__c to appConstantSettings.NumberField__c Commented Mar 18, 2014 at 12:56
  • My Bad. I mistakenly mentioned the object itself for null check. Commented Mar 18, 2014 at 14:05

1 Answer 1

2

1) appConstantSettings.NumberField__c.scale() will get you the number of digits behind the comma for a numeric (decimal) field.

2) system.today().addMonths(-1) will get you 1 month before today

9
  • I want right hand side part of a decimal number. For ex: 12.8 . I want 8. Scale function will give you 12 Commented Mar 18, 2014 at 11:22
  • No, in case of 12.8 then .scale() will give you 1. So in case Numberfield__c.scale() > 0 then it's fractional, else not. Commented Mar 18, 2014 at 12:03
  • @guy Presume you meant appConstantSettings.Numberfield__c.scale()?
    – Keith C
    Commented Mar 18, 2014 at 12:44
  • @Keith you're right, thanks. I updated my answer and also made a comment to the poster to update the code fragment Commented Mar 18, 2014 at 12:57
  • @guy : What should be the value of appConstantSettings.Numberfield__c, so that it won't be considered as fractional ? Commented Mar 18, 2014 at 19:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .