Sub topic 3
Passing parameters and returning data
This chapter assumes you can write basic Apex (SOQL, classes) and a Lightning web component with decorators. It also assumes you know what @AuraEnabled and cacheable=true do. Here you will see how values move between JavaScript and Apex in both directions.
How parameters travel
A component passes parameters as one JavaScript object. Each key must match an Apex parameter name exactly, including case. The order of the keys does not matter. The platform serializes the object to JSON and binds each key to the Apex parameter with the same name.
Here is a method with three parameters:
public with sharing class ContactSearchController {
@AuraEnabled(cacheable=true)
public static List<Contact> findContacts(Id accountId, String searchTerm, Integer maxRows) {
String pattern = '%' + (searchTerm == null ? '' : searchTerm) + '%';
Integer rowLimit = (maxRows == null || maxRows < 1) ? 20 : Math.min(maxRows, 200);
return [
SELECT Id, Name, Email, Birthdate
FROM Contact
WHERE AccountId = :accountId AND Name LIKE :pattern
WITH USER_MODE
ORDER BY Name
LIMIT :rowLimit
];
}
}The component calls it imperatively:
import { LightningElement, api } from 'lwc';
import findContacts from '@salesforce/apex/ContactSearchController.findContacts';
export default class ContactSearch extends LightningElement {
@api recordId;
contacts = [];
async handleSearch(event) {
const contacts = await findContacts({
accountId: this.recordId,
searchTerm: event.target.value,
maxRows: 10
});
this.contacts = contacts;
}
}Type mapping
JavaScript has fewer types than Apex, so the platform converts values for you.
| Apex type | JavaScript side |
|---|---|
| `String`, `Id` | string |
| `Integer`, `Decimal`, `Double` | number |
| `Boolean` | boolean |
| `Date` | string such as `"1990-04-12"` |
| `Datetime` | ISO 8601 string in UTC |
| `List<T>`, `Set<T>` | array |
| `Map<String, T>` | object with string keys |
| sObject | object with the queried fields |
Some behaviors follow from this table:
- An
Idparameter accepts a 15 or 18 character string. An invalid string fails before your method runs. - Send dates as strings, not
Dateobjects. Convert aDatetimewithnew Date(value)when you display it. - A field you did not query is absent from the returned object, so it reads as
undefinedin JavaScript. A related child query appears as an array under the relationship name, for exampleContacts. - An Apex
Decimalfield can come back with fewer digits than you stored, because JavaScript numbers are floating point. Return aStringif exact digits matter.
Wire parameters and missing values
With @wire, the parameter object is built from component properties. A value written as '$recordId' is reactive. If any referenced property is undefined, the wire does not call the server. This is useful while the page is still loading. A null is sent as null, so the method must handle it. The example above does so for searchTerm and maxRows.
What can be returned
A method can return a primitive, an sObject, a collection, or an instance of a class whose members are marked @AuraEnabled. A method returning void is allowed, and the JavaScript promise resolves to undefined. Everything returned is serialized to JSON, so keep results small. Very large payloads slow the page and can hit response size limits. The next chapter covers wrapper classes for results that need more than sObjects.
Parameters are separate from the return value. Passing an Apex class as a parameter works only if its members are @AuraEnabled, and the JavaScript object must use matching property names. A simple Map<String, Object> or several primitive parameters are often easier.
Common errors
- Misspelled key: the parameter binds as
null, so the method behaves as if nothing was passed. Check the key against the Apex signature first. - Wrong type: sending the string
"ten"for anIntegerfails with an error inerror.body.message. - Reading a field you did not query: you get
undefinedin JavaScript. In Apex it throwsSObjectException.
Exercise
A component calls findContacts({ accountid: this.recordId, searchTerm: 'Sm' }). The Apex method signature is findContacts(Id accountId, String searchTerm, Integer maxRows). What happens, and how do you fix it?
Show answer
The key accountid does not match accountId, because names are case-sensitive. accountId is therefore bound as null, and the query returns no contacts. maxRows is also not sent, so it is null and the method falls back to 20. Rename the key to accountId to fix it.