Getting length of integer part and fractional part in apex of a number type field salesforce

Sometimes we need to play with field (custom field or standard) properties in apex. If we need to find the integer part length in a “Number” type field, then there is no direct method in Describe Field Result (DescibeFieldResult) class. But we can find it using some other methods of DescribeFieldResult class.

Lets suppose we have a custom field of Number type with below properties.

“Test Number Field” (Test_Number_Field__c) Number(14, 2).

Number Options (Integer Part Length)
Number type field options.

So here from the above custom field we want to fetch Length property from number options i.e. 14. But when we use getPrecision() method we will get 16, and when we use getScale() method we will get 2.

Output of the above apex code snippet will be :

Apex Code Output.

So here we get the length of our Integer part in apex.

Conclusion

Integer Part Length = dfr.getPrecision() – dfr.getScale();

Fractional Part Length = dfr.getScale();

Total Length = dfr.getPrecision();

Finally if you need to do anything with the integer part length then grab the code from here and make it fast.

Happy Coding 🙂

Overlapping pseudo code

As a developer, we sometimes need to write code for checking overlapping. We check overlapping for two objects having one start and end point (Overlapping between two appointments, overlapping between two courses, programs etc).
For example lets take a Course as object, So Course has a Start Date and End Date.

Course (StartDate, EndDate).

Now we have given two courses, “courseOne” and “courseTwo”, now we need to check whether they are overlapping to each other or not?

We have For courseOne

  • courseOne.startDate
  • courseOne.endDate

For courseTwo

  • courseTwo.startDate
  • courseTwo.endDate
Analysis of Possible Overlapping

Let’s start some analysis on possible overlapping between courseOne and courseTwo, there are four possible use cases when courseOne and courseTwo can overlap each other.

Overlapping Analysis Image.
Overlapping Analysis

1.) Complete Overlap : When courseOne completely covers the courseTwo i.e. courseOne starts before courseTwo and ends after courseTwo. So in coding we can write this condition as.

2.) Under Overlap : When courseTwo completely covers the courseOne i.e. courseTwo starts before courseOne and ends after courseOne. We can simply say its vice-versa of complete overlap. In coding we can write this as.

3.) End Overlap : When ending of courseOne overlap with starting of courseTwo i.e. Before end of courseOne, courseTwo starts. We can write this check in code as.

4.) Start Overlap : When starting of courseOne is overlapped with ending of courseTwo i.e. courseOne starts before courseTwo ends. In coding we can write this conditions as.

Pseudo Code

Finally you can write a complete method to check overlapping between two object as follows.

This code can be changed as per your requirement, just grab this pseudo code and be a faster/smarter coder.
Happy Coding 🙂