0

I'm wondering if my syntax is off. My varCreditRefundAmount is a positive value and when I add it to a new transaction representing a refund, I want to convert it to a negative amount.

This is what I'm doing:

            newTransaction.ChargentOrders__Amount__c = varCreditRefundAmount * -1;

But I'm getting the error: Arithmetic expressions must use numeric arguments

Is it my syntax or could I be doing something else wrong? The value in the variable is a currency and the field I'm trying to send it to is also currency.

Thanks!

3
  • It'd help to have more context here. Can you edit your question to show us how varCreditRefundAmount is declared and set? Given the error, I suspect you've declared it as a String, but the less that other people have to guess the more accurate the answers you'll get.
    – Derek F
    Commented Dec 24, 2021 at 14:08
  • I suspect varCreditRefundAmount may be holding a string value that looks like a currency: '3.00' for example - otherwise your logic is fine :)
    – Matt Lacey
    Commented Dec 24, 2021 at 14:12
  • You are right, I'm passing it as a string. It's coming from a Flow and the input is currency in the flow so can I declare as currency in my apex or would it be integer?
    – Heather
    Commented Dec 24, 2021 at 14:16

1 Answer 1

2

Some languages allow a bit more fluidity in data types (weakly/loosely typed languages), but not Apex. Apex is "strongly typed", which means that there's basically no flexibility. If something expects to be passed something like an Integer or Decimal, you can't pass it a String instead.

That's the case here. You're trying to multiply a string and a number, and since that operation isn't defined, you get an error.

If you're taking a currency, then you'd probably want to store that as a Decimal (Double would likely also work, but I believe Decimal is the better fit).

Changing the type of the input to your invocable method to be Decimal would be the preferred approach, I think. If for some reason that isn't possible, as long as you don't have currency symbols or decimal separators in your string, (e.g. '3,000.00' and '$3000.00' would be bad, but '3000.00' is fine), then you can use the valueOf() method provided by the Decimal class to convert it to a decimal.

e.g. Decimal converted = Decimal.valueOf(givenInputString);

3
  • Thank you!! That seems to have solved my math error!
    – Heather
    Commented Dec 24, 2021 at 14:51
  • @Heather As a side note, you don't need to multiply by -1, you can also simply write -myDecimalVar or even -Decimal.valueOf(myDecimalString).
    – sfdcfox
    Commented Dec 24, 2021 at 15:21
  • @sfdcfox OK! Thanks for that tip!
    – Heather
    Commented Jan 5, 2022 at 17:12

You must log in to answer this question.

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