0

I am trying to pass parameter on page load in LWC. Is this correct way of passing parameter to apex?

import { LightningElement } from 'lwc';
import CurrentUserId from '@salesforce/user/Id';
import setData from '@salesforce/apex/myClass.setData';
export default class myClasstest extends LightningElement {
    connectedCallback(){
        console.log('CurrentUserId '+CurrentUserId);
        setData(CurrentUserId);
    }
}

1 Answer 1

0

You need to understand calling apex method imperatively in LWC. A sample method is below. Here, searchKey is the parameter name defined in apex. The imported function returns a promise. If your method returns any value, result has that value. if there is any error, it will go in catch block.

   connectedCallback() {
        findContacts({ searchKey: this.value })
            .then((result) => {
                this.contacts = result;
                this.error = undefined;
            })
            .catch((error) => {
                this.error = error;
                this.contacts = undefined;
            });
    }

Read more here:- Call Apex Methods Imperatively

Also, check out lwc recipes examples here:-

  1. apexImperativeMethod
  2. apexImperativeMethodWithComplexParams
  3. apexImperativeMethodWithParams

You must log in to answer this question.

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