I have a very simple LWC component with 2 input fields and a button that calls an Apex method. Unfortunately, I'm not able to reach to Apex class.

LWC Component:

    <lightning-input type="text" label="First Name" value={inputFirstName} onchange={updateFirstName}></lightning-input>
    <lightning-input type="text" label="Last Name" name="input3" value={inputLastName} onchange={updateLastName}></lightning-input>
    <lightning-button label="Login" onclick={handleAddRecord}></lightning-button>


import { LightningElement, track, api, wire } from 'lwc';
import tryAddRecord from '@salesforce/apex/MyClass.addRecord';

export default class myTestLwc extends LightningElement {
    @api inputFirstName;
    @api inputLastName;

    handleAddRecord() {
        console.log(this.inputFirstName + ' ' + this.inputLastName);
            firstName : this.inputFirstName, 
            lastName : this.inputLastName
        .then(result => {
            console.log('It is a success');
        .catch(error => {
            console.log('You got an error');

    updateFirstName(event) {
        this.inputFirstName = event.detail.value;

    updateLastName(event) {
        this.inputLastName = event.detail.value;

Apex Class:

public class MyClass {
    public static String addRecord(String firstName, String lastName) {
            System.debug(firstName + ' ' + lastName);
            return 'testString';
        catch (Exception ex) {
            return ex.getMessage();            

On button click, I have the correct values but I get console log "error" and I'm never hitting Apex method. What am I missing here?

  • What is the error? Is it a 401 error? If so, you need to enable the class for the profile you are running it as. First glance, you code looks fine, so I think it must be permissions. Mar 22, 2021 at 2:46

3 Answers 3


You have API enabled input variables - which means they are not modifiable from the app.

For now, just take away the @api annotations on your two classes and this will likely fix the issue.


If you do want to both set via an attribute, you could make a setter/getter:

set inputFirstName(value) {
  this._inputFirstName = value;
get inputFirstName() {
  return this._inputFirstName;
_inputFirstName = "";

If this is not the cause of the problem - first, examine the error - normally they tell you something useful, then check your profile settings for your class to make sure you are able to access it from LWC


Check your profiles/permission sets and ensure that the user you are viewing the LWC component as has access to the MyClass apex class.

I would try renaming 'tryAddRecord' to 'addRecord' in case lwc does something weird with names and want them to match with what is on apex (It's likely that this shouldn't matter but i'd try that anyway).

Log the Error you get from trying to make the call and post it here, that will help with identifying your issue


If you still facing the same issue, try adding "with sharing" keyword in declaration of your apex code.

like :

public with sharing class MyClass {};

In Apex, the "with sharing" keyword is used to enforce sharing rules when performing queries on Salesforce records. It's commonly used in the class definition to indicate that the class should respect the sharing settings of the current user executing the code. This means that the class will only be able to access records that the user has permission to see, based on the organization-wide defaults, role hierarchies, and sharing rules.

  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 22, 2023 at 6:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .