1

I have a controller with a apex method which takes a user id. This controller is used in a LWC component. Here is where I am trying to call the apex method via a wire method.

This is done in a Experience Site where I have placed the LWC component which is the only component on the page.

The user Id is taken using standard salesforce functionality and the method is imported.

import USER_ID from "@salesforce/user/Id";
import updateStatusOpen from '@salesforce/apex/UpdateFormRequestStatusController.updateFormRequestStatusOpened';

The controller method:

@AuraEnabled(cacheable=true)
public static void updateFormRequestStatusOpened(Id userId) {
    System.debug('Here ' + userId);
    System.debug('here ' + userinfo.getProfileid());
    System.debug('here ' + UserInfo.getUserId());
}

Right now the console debugs are 'In Lwc' amd 'outside' its like it just skips calling the method.

@wire(updateStatusOpen, { userId: '$userId' })
wiredCustomObjects({ error, data }) {
    console.log('In LWC');
    if (data) {
        console.log('Success');

    } else if (error) {
        console.error('Error loading custom objects', error);
    }
    console.log('outsides');
}

There are no logs in the Developer Console which is why I think the method is not being called at all. I have also tried running the method imperatively and no error logs are displayed. It also calls the wire adapter twice.

Full code

import { LightningElement, wire } from 'lwc'; import USER_ID from "@salesforce/user/Id"; import updateStatusOpen from '@salesforce/apex/UpdateFormRequestStatusController.updateFormRequestStatusOpened';

export default class ExperienceFormRequestStatusOpened extends LightningElement {

userId = USER_ID;
wiredCustomObjects;

connectedCallback() {
    console.log('connected callback');
    updateStatusOpen({userId: this.userId })
        .then(result => {
            console.log('inside connected callback ' + this.userId);
            console.log(result);    

        })
        .catch(error => {
            console.error('Error fetching data: ', error);
        });
}

}

1 Answer 1

3
@wire(updateStatusOpen, { userId: '$userId' })

should be:

@wire(updateStatusOpen, { userId: USER_ID })

The '$fieldName' format is only for variables in the class.

However, be aware you can't perform DML in this configuration, which I think you may be trying to do.

Instead, call it imperatively:

@AuraEnabled(cacheable=false)
public static void updateFormRequestStatusOpened(Id userId) {
    System.debug('Here ' + userId);
    System.debug('here ' + userinfo.getProfileid());
    System.debug('here ' + UserInfo.getUserId());
}
connectedCallback() {
  updateStatusOpen({ userId: USER_ID });
}
3
  • Thanks for the reply. I should have shown that part of the code. I am initializing the variable userId. But I tried calling it imperatively and the method still did not run. The user does have access to the class as well.
    – Liam
    Commented May 15 at 17:54
  • @Liam Please edit your question to show the code you're talking about, it'll help visualize it.
    – sfdcfox
    Commented May 15 at 18:01
  • Sorry the start of the code is a bit wonky I couldn't format it. The controller I have already placed above its just a class with that method.
    – Liam
    Commented May 15 at 19:16

You must log in to answer this question.

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