For Lightning web component, I'm following the Apex imperative pattern described here but the result in the LWC is always undefined even though I know the Apex method returns values.

I have an LWC that takes some input, then I instantiate an object from the data and pass it to an Apex method. I expect to get an array of contacts in return, but I always get undefined.

When I call the Apex method directly in execute anonymous, I get a Contact list, so I know the method is running correctly.

Is there something wrong with the object I'm passing to the Apex method, or am I doing something wrong with the result from the method call?

Here is the HTML:

    <lightning-input data-formfield="firstName" label="First Name" required></lightning-input>
    <lightning-input data-formfield="lastName" label="Last Name" required></lightning-input>
    <lightning-input data-formfield="mobile" label="Mobile" type="phone" class="spec-req"></lightning-input>
    <lightning-input data-formfield="email" label="Email" type="email" class="spec-req"></lightning-input>
    <lightning-button label="Submit" click={handleFirstSubmit}></lightning-button>

Here is the JS:

import { LightningElement, track, api, wire } from 'lwc';
import findContacts from '@salesforce/apex/TMC_ContactServices.findContacts';

export default class BatchRegistration extends LightningElement {
    @track foundContacts;
    @track submittedContact;

    // Handle the initial contact entry.
    handleFirstSubmit() {
        // Build contact record
        let submittedContact = { 'sObjectType': 'Contact' };
        submittedContact.FirstName = this.template.querySelector('lightning-input[data-formfield="firstName"]').value;
        submittedContact.LastName = this.template.querySelector('lightning-input[data-formfield="firstName"]').value;
        submittedContact.Email = this.template.querySelector('lightning-input[data-formfield="email"]').value;
        submittedContact.MobilePhone = this.template.querySelector('lightning-input[data-formfield="mobile"]').value;
        console.log('submittedContact ' + JSON.stringify(submittedContact));

        // Call the Apex method to find existing contacts
        findContacts({ con: submittedContact })
        .then(result => {
            console.log('result ' + JSON.stringify(result));
            this.foundContacts = result;
        .catch (error => {
            console.log('error ' + error.message);

Here is the Apex class:

public without sharing class TMC_ContactServices {
    public static List<Contact> findContacts(Contact con) {
        List<Contact> foundInitialContacts = new List<Contact>();

        // Use duplicate rules here to find existing contacts based on the con parameter.
        // This populates the foundInitialContacts list but it has IDs only, so we have to re-select to get the desired fields.

        // Now we select the fields we need for the duplicate list and return it.
        if (!foundInitialContacts.isEmpty()) {
            return [SELECT FirstName, LastName, Email, MobilePhone, MailingPostalCode FROM Contact
                    WHERE Id IN :foundInitialContacts];
        } else {
            return null;
  • Have you tried sending this as a string instead of an sObject, and deserializing on Apex directly? Aug 20, 2019 at 22:28
  • As an aside, I recommend that you Say "No" To "Null". It simply adds verbosity to your code for no reason.
    – sfdcfox
    Aug 20, 2019 at 22:48
  • You seem to be sending the field values from JS but not utilizing those in your apex. How are you searching for contacts in your apex? What is foundInitialContacts's value?
    – Jayant Das
    Aug 20, 2019 at 23:21
  • @JayantDas I am using the Contact that is passed in, I just omitted the code for brevity. The duplicate rules code populates the foundInitialContacts but they have IDs only, no field values. Aug 20, 2019 at 23:27
  • Does your apex receive the inputs from your component as expected? Have you tried putting some debug logs to verify if apex works as expected?
    – Jayant Das
    Aug 20, 2019 at 23:36

1 Answer 1


Arrgh, I was mapping the firstName datafield into the LastName field in the submittedContact. I fixed it and it works.

You must log in to answer this question.

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