0

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

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.

  • 1
    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 Feb 9 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.