Closed rodriggj closed 2 years ago
View / Command Palette / SFDX: Create Apex Class
CarController
, hit Enter
Enter
CarController
CarController.cls
CarController.cls-meta.xml
CORRECTION: The picture is incorrect with the naming ... it utilizes a
carController
vs. aCarController
naming convention. Please use theCarController
style.
CarController.cls
file and enter the following code. The code is simply implementing a public
static
method, which means it can be invoked without creating an instance of the CarController
class. And what it will do is execute a SOQL query to the Salesforce database to retrieve the specified fields from the Car__c
Object. public with sharing class CarController {
public static List<Car__c> getCars(){
return [SELECT Id, Name, MSRP__c, Description__c, Category__c, Picture_URL__c FROM Car__c];
}
}
public with sharing class CarController {
@AuraEnabled(Cacheable=true)
public static List<Car__c> getCars(){
return [SELECT Id, Name, MSRP__c, Description__c, Category__c, Picture_URL__c FROM Car__c];
}
}
classes
directory and selecting SFDX: Deploy Source to Org
Now we have to use this getCars()
method in our carTileList
lwc / carTileList.js
fileimport
the getCars()
method from the CarController.cls
file. getCars
method, use the @wire
adaptor to call the getCars
method. Since you are not passing anything you don't have a second argument to the @wire
. carsHandler()
and pass 2 parameters, data
& error
-- and we will pass these params to console.log() to view what the data returns (which should be our Cars Objects) if not display an error message. import { LightningElement, wire } from 'lwc';
import getCars from '@salesforce/apex/CarController.getCars'
export default class CarTileList extends LightningElement {
@wire(getCars)
carsHandler({data, error}){
if(data){
console.log(data)
}
if(error){
console.error(error)
}
}
}
carTileList
folder and deploy the changes to your DevOrg.
Final step here is to create 2 properties with our carTileList
class
cars
error
Now we can assign the data
response or the error
to properties that we can use data binding
for and pass to our front-end HTML.
carTileList.js
import { LightningElement, wire } from 'lwc';
import getCars from '@salesforce/apex/CarController.getCars'
export default class CarTileList extends LightningElement {
cars
error
@wire(getCars)
carsHandler({data, error}){
if(data){
console.log(data)
this.cars = data
}
if(error){
console.error(error)
this.error = error
}
}
}
Deploy changes by right-clicking on parent carTileList
directory, and selecting SFDX: Deploy to Source Org