Lightning Components & AuraEnabled method parameters: What’s working and what’s not?

Lightning Components & AuraEnabled Method : What’s Working & What’s Not?

Parameters Passing from Client-Side to Server-Side is the much-needed part for Lightning component development.

Prerequisite to pass a parameter from client-side to server-side:

  1. Value of a Lightning component attribute.
  2. Server-side auraEnabled Parametrized action.
  3. Client-side Javascript action that calls server-side apex action.

To understand the calling of Apex Method from a Lightning Component Client Controller, Read this article.

In Salesforce Lightning Framework the same data type for client-side attribute and server-side argument does not work correctly.

Below are some data types that do not work correctly in Lightning Framework:

Integer:

If you set a server-side Integer argument with a client-side Integer attribute it doesn’t work correctly.

@AuraEnabled
public static void setInteger(Integer myInteger) {
    System.debug(myInteger);
}

When the above code executes, it executes correctly but if you perform some mathematical operation on this integer variable a server error comes FATAL_ERROR Internal Salesforce.com Error.

The reason for the error is that the integer variable has not correctly cast by the framework. You are bound to typecast the integer variable in your apex code. Add the below code in your apex method it will work fine :

myInteger = Integer.valueOf(myInteger);
myInteger++;

sObjects:

sObjects can correctly pass from server-side to client-side but they don’t work correctly if your sObjects have child relationships records, the child relationships won’t be available in the Apex method.

Date:

Next, the date data type attribute also not correctly work if passed from the client-side to the server-side. After being set from the client-side, the server-side date parameter ends up being null.

<aura:attribute name="”myDate”" type="”Date”" default="”1981–08–26″/"></aura:attribute>
@AuraEnabled
public static void setDate(Date myDate) {
    System.debug(myDate); // –&gt; null 
}

The best replacement for this is - send the date as a string and then typecast it into date datatype in apex.

Wrapper Class:

Wrapper class type attribute can also be pass from the server-side to the client-side. But to pass a wrapper class type from the server-side to the client-side first serialize the attribute at client-side and then deserialize it at the server-side.

Set:

You cannot use a set parameter in an AuraEnabled method. Whenever a class with AuraEnabled method that has set as a parameter is saved an error message comes:

Parameter type does not support AuraEnabled.

ParameterPassing1

Popular Salesforce Blogs