0

I have to perform some calculations on two currency field in the following manner and display in the UI:

field1*field2/field2

So i have the following code for this:

Decimal value1 = object.field1;
Decimal value2 = object.field2;
Decimal result = value1*value2/value2;

But this is throwing error:Invalid type.

  • You do realize value1*value2/value2 == value1 right? – Adrian Larson Sep 6 '16 at 12:52
  • Unless value2 is 0 or null, in which case the calculation will throw errors. – Adrian Larson Sep 6 '16 at 13:11
  • oops, my bad. The formula is (value1-value2)/value2. – starhunter Sep 6 '16 at 13:41
1

Can you please verify that your fields are indeed Currency fields? This operation (although as correctly stated by Adrian), this operation does not make sense, should work with Decimals.

With System.debug(object.field1 instanceof Decimal); you can test if object.field1 indeed is a Decimal.

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.