1

A very basic question.

We define a number type of custom field and try to fetch it in apex. After this suppose I write following code :

Set<Integer> validCounts= new Set<Integer>{1,3,6,9};
validCounts.contains(caseRecord.Count__c); //field of type: Number(5,0)
//Gives error: Method does not exist or incorrect signature: void contains(Decimal)     
//from the type Set<Integer>

What is best solution for this:

  1. We should define set as Set<Decimal> OR

  2. Typecast field value to integer?

2 Answers 2

3

My opinion is that if Count__c is logically always an integer, then your business logic should reflect that.

So stick with Set<Integer> and go with e.g.:

validCounts.contains(caseRecord.Count__c != null ? caseRecord.Count__c.intValue() : null);

This also avoids you (and anyone else maintaining the code in the future) needing to know how sets handle decimals that have different numbers of decimal places.

5
  • 2
    Also, from a practical perspective, Decimal uses like 36 bytes of memory, but Integer uses only 4, which can easily be the difference between a heap limit exception or not.
    – sfdcfox
    Commented Feb 9, 2019 at 14:50
  • @sfdcfox I initially posted this trying to tag Keith C but I guess it didn't work maybe because of the space in his username, so reposting again tagging the real master, I was wondering if there are any differences between using the intValue() method from the Decimal class vs using Integer.valueOf() method and also wouldn't the second one help eliminate the null check? Thanks!
    – Bahman.A
    Commented Jul 1, 2020 at 16:57
  • 1
    @Bahman.A In SFSE, you have to ignore any spaces to mention someone (so KeithC to mention Keith). Also, comments on a post automatically notify the creator of the post (in fact, you can't mention the post creator explicitly). Aside from that, for your question, yes, Integer.valueOf will work even with null values, so validCounts.contains(Integer.valueOf(caseRecord.Count__c)); is just as efficient with no risk of NullPointerException.
    – sfdcfox
    Commented Jul 1, 2020 at 17:08
  • @sfdcfox Awesome to know that info about SFSE. and appreciate the quick answer. Thank you, sir!
    – Bahman.A
    Commented Jul 1, 2020 at 17:10
  • 2
    @Bahman.A Though, thinking about it, you can also just validCounts.contains((Integer)caseRecord.Count__c), as casting a null value is also okay, and performs better CPU-wise.
    – sfdcfox
    Commented Jul 1, 2020 at 17:11
0

This is interesting. I would like to format the variable based on the field type. Now I am wondering what field types will be true for the 2nd condition (instanceOf Integer) because even the Number type fields are stored as Decimal. For example QuoteLineItem.Quantity prints 2.00 and not 2 in Apex.

There is no Decimal method than can differentiate between 2.00 (Number) and 2.00 (Decimal).

if(val instanceOf Decimal || val instanceOf Double) {
  val = Decimal.valueOf(Double.valueOf(val)).setScale(2).format();
} else if(val instanceOf Integer) {
  val = Integer.valueOf(val).format();
}

You must log in to answer this question.

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