Loading

Convert a Currency Field Value to an Integer Using Salesforce Apex

Publiceringsdatum: Jun 26, 2026
Beskrivning

In Salesforce Apex, a Currency field stores monetary values as a Decimal data type. When you need to use that value as an Integer — for example, when adding it to an Integer list, performing integer arithmetic, or passing it to a method that expects an Integer — you must explicitly convert it using the Integer.valueOf() method. Note that this conversion truncates any decimal portion of the value (it does not round).

Lösning

How to Convert a Currency Field to an Integer in Salesforce Apex

Use the Integer.valueOf() method to convert a Currency field value (stored internally as a Decimal) to an Integer.
For example, the AnnualRevenue field on the Salesforce Account object is a Currency field. To convert this value to an Integer, first query the field from the Account object, then call Integer.valueOf() on the returned value. The result can then be stored in an Integer variable or added to an Integer list.
The conversion returns only the whole-number portion of the value. For example, if AnnualRevenue is 5000.75, Integer.valueOf(5000.75) returns 5000.

This example is illustrative and may not satisfy all business needs. Modify it as required to fit your specific use case.

Account acc1 = [select AnnualRevenue from account where AnnualRevenue != null limit 1];

System.debug(‘Annual Revenue: ' + acc1.AnnualRevenue);

list<integer> quarterBonus = new list<integer>(1);
quarterBonus.add(0,integer.valueOf(acc1.AnnualRevenue));

system.debug('Quarter Bonus: ' + quarterBonus[0] );
Knowledge-artikelnummer

000387949

 
Laddar
Salesforce Help | Article