Sub topic 4
Passing parameters
Prerequisites
This chapter assumes you can write an @AuraEnabled Apex method and import it into a component, as covered in the previous chapters. You should also be comfortable with JavaScript objects and async/await.
The rule: one object, keyed by parameter name
An imported Apex method is a JavaScript function that takes a single argument. That argument is an object. Each key must match the name of an Apex parameter exactly, and each value is the data for that parameter.
Here is the Apex signature from the earlier chapters:
@AuraEnabled(cacheable=true)
public static List<Contact> searchContacts(String searchTerm) {
String key = '%' + searchTerm + '%';
return [
SELECT Id, Name, Email
FROM Contact
WHERE Name LIKE :key
WITH USER_MODE
ORDER BY Name
LIMIT 20
];
}The JavaScript call passes { searchTerm: ... }:
this.contacts = await searchContacts({ searchTerm: this.searchTerm });The key searchTerm is matched to the Apex parameter searchTerm. Position does not matter, and the names are case-sensitive.
Multiple parameters
Add one key per parameter. The order of keys is irrelevant.
@AuraEnabled
public static void updateEmail(Id contactId, String email) {
try {
Contact c = new Contact(Id = contactId, Email = email);
update as user c;
} catch (DmlException e) {
throw new AuraHandledException(e.getDmlMessage(0));
}
}await updateEmail({ contactId: this.recordId, email: this.newEmail });Type conversion
Values are serialized to JSON on the client and deserialized into the Apex type on the server. These mappings cover most cases:
| JavaScript value | Apex parameter type |
|---|---|
| string | `String`, `Id` |
| number | `Integer`, `Decimal`, `Double` |
| boolean | `Boolean` |
| array | `List<T>`, `Set<T>` |
| plain object | `Map<String, Object>` or an Apex class with `@AuraEnabled` fields |
| ISO 8601 string | `Date`, `Datetime` |
An Id parameter accepts a string. If the string is not a valid Id, the call fails on the server with a type conversion error.
Passing a custom object
To send structured data, define an Apex class whose fields are marked @AuraEnabled. Then pass a plain object with matching field names.
public with sharing class ContactController {
public class EmailUpdate {
@AuraEnabled public Id contactId;
@AuraEnabled public String email;
}
@AuraEnabled
public static void updateEmails(List<EmailUpdate> updates) {
List<Contact> toUpdate = new List<Contact>();
for (EmailUpdate u : updates) {
toUpdate.add(new Contact(Id = u.contactId, Email = u.email));
}
update as user toUpdate;
}
}await updateEmails({
updates: [
{ contactId: '003000000000001AAA', email: '[email protected]' }
]
});The outer key updates is the parameter name. Each inner object follows the class fields.
Common errors
- Passing the value directly.
searchContacts(this.searchTerm)does not work. The function expects an object, so the parameter is never bound. - A misspelled or wrong-case key.
{ searchterm: 'Ada' }does not matchsearchTerm. The Apex parameter is thennull, and no compile-time error warns you. - Passing
undefined. A key whose value isundefinedis dropped during serialization, so Apex receivesnull. Guard againstnullin Apex before using the value, for example in string concatenation or aLIKEfilter. - Passing a reactive proxy. Values from
@trackobjects or from@apiproperties may be read-only proxies. Usually they serialize fine. If you hit an error, copy the data first withJSON.parse(JSON.stringify(value)).
Reading values at call time
Read parameters from component state when the call is made, not earlier. In the sample component, handleTermChange stores the input text in this.searchTerm, and handleSearch reads it when the button is clicked. This keeps the call using the latest value.
Exercise
A component calls getAccounts({ Name: this.name }), but the Apex method is declared as getAccounts(String name). The method always sees null. What is wrong, and how do you fix it?
Show answer
The key Name does not match the Apex parameter name name, and keys are case-sensitive, so the parameter is never bound. Change the call to getAccounts({ name: this.name }).
Exercise
Write the JavaScript call for the Apex method @AuraEnabled public static void assign(Id caseId, List<Id> userIds), using this.caseId and this.selectedUsers.
Show answer
await assign({ caseId: this.caseId, userIds: this.selectedUsers }); The object has one key per Apex parameter, and the array maps to List<Id>.