I have the simple app which multiplies Decimal
number a
on Integer
number b
.
The simple app has the following code:
<aura:application controller="ReproduceIssueController" >
<aura:attribute name="aa" type="Decimal" default="0.15" description="Decimal number a"/>
<aura:attribute name="bb" type="Integer" default="10" description="Integer number b"/>
<aura:attribute name="cc" type="Decimal" description="Decimal number a"/>
<lightning:input type="number" step="0.01" label="Decimal Number A" value="{!v.aa}"/>
<lightning:input type="number" step="1" label="Integer number B" value="{!v.bb}"/>
<lightning:button variant="brand" label="Multiply" onclick="{! c.m }" />
<br/>
Multiplication result <ui:outputText value="{!v.cc}" />
</aura:application>
The app has the following Javascript controller
({
m : function(component, event, helper) {
var action = component.get("c.mu");
action.setParams( {
a : component.get("v.aa"),
b : component.get("v.bb")
});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
component.set('v.cc', response.getReturnValue() );
alert("From server: " + response.getReturnValue());
}
else if (state === "INCOMPLETE") {
// do something
}
else if (state === "ERROR") {
var errors = response.getError();
if (errors) {
if (errors[0] && errors[0].message) {
console.log("Error message: " +
errors[0].message);
}
} else {
console.log("Unknown error");
}
}
});
$A.enqueueAction(action);
}
})
and the following Apex Server-side controller
public class ReproduceIssueController {
@auraEnabled
public static Decimal mu(Decimal a, Integer b){
return a * b;
}
}
When I try to execute it I see the following error messages in Javascript
Error message: An internal server error has occurred Error ID:
109932382-3746 (119852647)
and the following error in Developer Console logs
System.UnexpectedException: Illegal argumentsClass.ReproduceIssueController.mu: line 4, column 1
Apparently it fails on multiplication line but it is not clear why.
Cannot Lightning handle and pass numbers correctly? Do we have to pass only strings between the Lightning front end application and Apex Server-side controller?
Best Answer
Update:
As far as I can see now in Spring 2019 version, this issue can no longer be reproduced even if I set API Version to older one for both the Lightning Application and Apex Controller. The issue seems to be fixed now, so no workaround anymore needed (like workaround described below to change signature of Apex method to Decimal mu(Decimal a, Decimal b).
Old issue explanation and workaround:
It is not obvious reading the Salesforce documentation
that Lightning passes every number as
Decimal
typed value disregarding if it is actuallyInteger
orDecimal
.This becomes clear when you implement
getType
method and debug type of the input variablesWhen you look at debug logs you will see that types of both input variables are
Decimal
, notInteger
,despite one might expect type of second input variable to be
Integer
since its attribute is declared asInteger
, not asDecimal
.So, the solution for this would be change input variable type to
Decimal
, if you make this simple changeyou will see that the code executes successfully
So you don't have always convert any value to String to pass it from Lightning to Apex, sometimes it is enough just to have declaration change.