Plat-Dev-301 practice question 6 of 10

Refer to the code below: Lightning Web Component JS file JavaScript import {LightningElement} from 'lwc'; import serverEcho from…

Full set: 160 questions
Refer to the code below: Lightning Web Component JS file JavaScript import {LightningElement} from 'lwc'; import serverEcho from '@salesforce/apex/SimpleServerSideController.serverEcho'; export default class Helloworld extends LightningElement { firstName = 'world'; handleClick() { serverEcho({ firstName: this.firstName }) .then((result) => { alert('From server: ' + result); }) .catch((error) => { console.error(error); }); } } Apex Controller Java public with sharing class SimpleServerSideController { @AuraEnabled public static String serverEcho(sObject firstName) { String firstNameStr = (String)firstName.get('firstName'); return ('Hello from the server, ' + firstNameStr); } } Given the code above, which two changes need to be made in the Apex Controller for the code to work?
Answer options
A: Annotate the entire class as @AuraEnabled instead of just the single method.
B: Change the argument in the Apex Controller line 05 from sObject to String. Correct answer
C: Remove line 06 from the Apex Controller and instead use firstName in the return on line 07. Correct answer
D: Change the method signature to be global static, not public static.
Correct Answer:
B. Change the argument in the Apex Controller line 05 from sObject to String.; C. Remove line 06 from the Apex Controller and instead use firstName in the return on line 07.
Explanation

The Lightning Web Component passes a JavaScript object { firstName: 'someValue' } to the Apex method. The Apex method signature must correctly map to this structure.
1. The parameter type should be String, and the parameter name must match the key in the JavaScript object (firstName). Therefore, the signature must be changed from serverEcho(sObject firstName) to serverEcho(String firstName).
2. With the corrected signature, the firstName parameter directly holds the string value. The line String firstNameStr = (String)firstName.get('firstName'); is now incorrect and unnecessary, as a String variable has no .get() method. This line must be removed, and the firstName parameter should be used directly in the return statement.

Why Incorrect

A. Annotating the entire class with @AuraEnabled is a valid alternative to annotating each method, but it is not a required change for the existing code to work.
D. The public access modifier is sufficient for a method to be called from a Lightning component in the same namespace. global is not required.

References

1. Salesforce Developers Documentation, Lightning Web Components Dev Guide, "Call Apex Methods", Section: "Pass Parameters to Apex Methods". This guide shows that when passing a JavaScript object, the properties map to the named parameters in the Apex method.

2. Salesforce Developers Documentation, Apex Developer Guide, "AuraEnabled Annotation", Section: "Using the @AuraEnabled Annotation". This documentation confirms that @AuraEnabled can be used on static methods and that public or global access modifiers are required. It does not mandate global.

3. Salesforce Developers Documentation, Apex Developer Guide, "Data Types". This reference details the methods available for primitive types like String, confirming it does not have a .get() method, unlike sObject.

Scroll to Top