0

I am getting Illegal assignment from Decimal to Integer error for the code

Integer maxEmailsSentPerDay = tpfs.Maximum_Emails_Per_Day__c;

where Maximum_Emails_Per_Day__c is a number(18,0) field from a custom setting

2 Answers 2

3

Excerpt from API Data Types and Salesforce Field Types

Generally, API data types and field types in the user interface have the same names. For example, a date field is represented by a date data type in the API. However, some field types are represented differently depending on whether you are inspecting an object via the API or the user interface.

So, in your case even though your field type is set as Number(18,0), the API data type for it is Decimal i.e. although on interface it will be displayed as Integer its actual API data type will be Decimal, so it will be considered as Decimal

Similarly Currency, Formula, Number, Percent, and Roll-up summary types are considered as Decimal i.e. their API data type is Decimal.

Hence when you try to assign any of the above types to an Integer, you will have to typecast it or parse it. So, in your case, you can use any of the methods to either typecast or parse your field's value

Integer maxEmailsSentPerDay = Integer.ValueOf(tpfs.Maximum_Emails_Per_Day__c);

or

Integer maxEmailsSentPerDay = tpfs.Maximum_Emails_Per_Day__c.intValue(); //also check for null

or

Integer maxEmailsSentPerDay = (Integer) tpfs.Maximum_Emails_Per_Day__c;
0
0

Try below code

Integer maxEmailsSentPerDay = Integer.ValueOf(tpfs.Maximum_Emails_Per_Day__c);
2
  • Just for Curiosity, both the variable and field were of number data type. what was the reason behind the error? Mar 4, 2020 at 10:51
  • 1
    @AdityaPhadke The other answer goes in to much better detail--you may want to give that one a read. Also why it's a good habit on here to wait a bit before accepting the first answer, as given a bit of time, a better one might come along.
    – sfdcfox
    Mar 4, 2020 at 15:00

You must log in to answer this question.

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