[SalesForce] Lightning Web Component Push Failed

i import the method that i have to use in JavaScript file like this

import getRoles from '@salesforce/apex/LeaveSettingsController.getRoles';

@wire(getRoles)
rolesList;

when i'm trying to push the code to my scratch org i got an error like this
Unable to find Apex action class referenced as 'LeaveSettingsController'

I create an apex class in my DevHub org

public with sharing class LeaveSettingsController {

    public LeaveSettingsController(){

    }

    @AuraEnabled(cacheable=true)
    public static List<UserRole> getRoles(){
        return [SELECT Id, Name FROM UserRole];
    }
}

I have a DevHub org and only one scratch org and i use this command to push the code to the scratch org using the VS Code:

sfdx force:source:push

Best Answer

First, you need to create an apex class using below command from command palette,

sfdx: create apex class

You need to enter your class name, then just press enter to select default directory for apex classes.

A class will be created in your project in a folder like ...\default\classes.

Just paste all your code over there.

Then you can push to scratch org, which will create an apex class in your scratch org, and you won't get any error.

Update - An alternate way: If scratch org is open in the browser, you can also create apex class from there by going to setup -> Apex Classes.

Related Topic