Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Data Science
  3. Question
Data Science

SObject Numeric field - Integer.valueOf() vs casting

Asked by Himanshu Apr 15, 2021 1.1K views 1 answer
Share

About this question

When working in Apex it is not uncommon for us to work with a numeric field that has a 0 scale. To use that with some built in functions we need to use an Integer. To provide an example, let's say we have a field on Account TrainingDays__c. This field always holds a whole number. To use this field with Date.addDays() we need to convert the Decimal value that acct.TrainingDays__c is represented in to an Integer. For this task we have two options: Integer.valueOf(acct.TrainingDays__c) or (Integer)acct.TrainingDays__c. I know that there are some differences when working in other languages, but for the given example where the source value is a numeric SObject field: Is there ever a time when one of the methods would throw an exception and the other would not? Is one more performant than the other?Other than code consistency is there any reason to standardize on one approach?

Your answer

1 Answer

More Data Science discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Data Science Blogs

Guides, tips and career advice on Data Science from JanBask experts.