This Badge Requires a New Custom AI Playground
You’ll have limited time to complete this badge and any other badge requiring a AI Playground. If you run out of time, you’ll lose access to this Playground and may need to start over.
Create a Models API Lightning Web Component
Learning Objectives
After completing this unit, you’ll be able to:
- Create a custom Apex class.
- Build a Lightning web component.
- Deploy a Lightning web component to your AI Playground.
- Edit an org page with Lightning App Builder.
Now that you’ve set up your environment, it’s time to add functionality to the Lightning Web Component. In this unit, we follow along with Maria as she creates a custom Apex class, designs the UI, adds functional JavaScript, and deploys her Models API dashboard to the AI Playground.
Developer Doc Writer Example Use Case
This video shows you how to build a Lightning web component that accesses the Models API, using a basic example similar to the one in this module.
Create Models API Apex Classes
- In VS Code, right-click the classes folder located under force-app/main and select SDFX: Create Apex Class. Name the class
DashboardController
.
- New Apex classes in VS Code come with five lines of default code. Remove this default code. You won’t need any default code for this module since we provide complete code samples for both Apex classes and Lightning web component files.
- In the Apex class file,
DashboardController.cls
, copy and paste the following code.
- Save the file.
Create a New Class for Grounding Data
For Maria’s Models API dashboard to be functional, she needs to ground the LLM with relevant housing market data. In this step, we create a new class and populate it with data so that it may be referenced by the DashboardController
class.
For demonstration purposes, the data is a local file within the LWC. In an official implementation of this dashboard, the data in this step would be dynamically retrieved from a housing market API.
- In VS Code, right-click the classes folder and select SDFX: Create Apex Class. Name the class
HousingData
.
- In the Apex class file,
HousingData.cls
, copy and paste the following code.
- Save the file.
- Right-click the classes folder again, and select SFDX: Deploy This Source to Org.
- In the Output tab of the integrated terminal, view the results of your deployment. If the command ran successfully, a Deployed Source message lists the four files that were uploaded to the org.
Build a Models API Lightning Web Component
- In Visual Studio Code, open the Command Palette by pressing Ctrl+Shift+P (Windows) or Cmd+Shift+P (macOS).
- Type
SFDX
.
- Select SFDX: Create Lightning Web Component.
- Enter
modelsAPIDashboard
for the name of the new component.
- Press Enter to accept the default
force-app/main/default/lwc
.
- Press Enter.
- View the newly created files in the
modelsAPIDashboard
folder located underlwc
.
- In the HTML file,
modelsAPIDashboard.html
, copy and paste the following code.
- Save the file.
- Right-click the
modelsAPIDashboard
folder and select New File.
- Enter
modelsAPIDashboard.css
as the file name.
- In the CSS file,
modelsAPIDashboard.css
, copy and paste the following code.
- Save the file.
- In the JavaScript file,
modelsAPIDashboard.js
, copy and paste the following code.
- Save the file.
Let’s make our component available from various Lightning Experience pages by adding those targets to the metadata file.
- In the XML file,
modelsAPIDashboard.js-meta.xml
, copy and paste the following code.
- Save the file by pressing Ctrl+S (Windows) or Cmd+S (macOS).
Deploy to Your AI Playground
- Right-click the
modelsAPIDashboard
folder underlwc
.
- Click SFDX: Deploy This Source to Org.
- In the Output tab of the integrated terminal, view the results of your deployment. If the command ran successfully, a Deployed Source message lists the four files that were uploaded to the org.
Edit Page in Lightning App Builder
- In Visual Studio Code, open the Command Palette by pressing Ctrl+Shift+P (Windows) or Cmd+Shift+P (macOS).
- Type
SFDX
.
- Select SFDX: Open Default Org.
This opens your AI Playground in a separate browser.
- Click the gear icon (
) select Setup.
- In Quick Find, enter
Home
, then select Home in the Feature Settings section.
- For Advanced Seller Home, toggle the setting to Inactive.
- From the App Launcher (
), find and select Sales.
- Click the gear icon (
) select Edit Page.
- Scroll to the Custom section at the bottom of the Components list.
- Drag the modelsAPIDashboard Lightning web component from the Custom area of the Lightning Components list to the top of the Page Canvas.
- Click Save.
- Click Activate.
- Click Assign as Org Default.
- Click Save.
- Click Save again, then click the back button (
) to return to the page.
- Refresh the page to view your new component.
Congratulations on creating your first Models API Lightning web component!
Now DreamHouse Realty has a dashboard component directly on the Sales Home Page. With this dashboard the sales team can stay aware of current housing market trends and better understand the needs of their homebuyers. This example component is very simplistic, but with this implementation you can see the power of using the Models API to combine your business use cases with generative AI.