SimilarPropertyController
and then hit return to save it into the default location.Replace the default code with:
public with sharing class SimilarPropertyController {
public static List<Property__c> findProperties (Id recordId, Decimal priceRange) {
List<Property__c> property = [SELECT Price__c FROM Property__c WHERE Id=:recordId];
Decimal propertyPrice = property[0].Price__c;
List<Property__c> similarProperties = getSimilarProperties(recordId, priceRange, propertyPrice);
return similarProperties;
}
private static List<Property__c> getSimilarProperties (Id recordId, Decimal priceRange, Decimal price ) {
Decimal range;
if (priceRange == null) {
range = 100000;
} else {
range = priceRange;
}
return [
SELECT Id, Name, Beds__c, Baths__c, Price__c, Broker__c, Status__c, Thumbnail__c
FROM Property__c WHERE Id != :recordId AND Price__c > :price - range AND Price__c < :price + range
];
}
}
@AuraEnabled(cacheable=true)
on the blank line above the findProperties
method.similarProperties
.Paste the following code into similarProperties.html inside the <template>
tag:
<lightning-card title="Similar Properties"
icon-name="custom:custom85">
<div class="slds-m-around_medium">
<ul class="slds-list_vertical slds-has-dividers_top-space">
<template for:each={props.data} for:item="item" if:true={props.data}>
<li key={item.Id} class="slds-list__item">
{item.Name}
</li>
</template>
<template if:true={props.error}>
<li class="slds-list__item"><h3 class="slds-text-heading_small slds-text-color_error">{props.error}</h3></li>
</template>
</ul>
</div>
</lightning-card>
api,track,wire
to the import from lwc
in similarProperties.js.Add the following on a new line on line 2:
import findProperties from '@salesforce/apex/SimilarPropertyController.findProperties';
Add the following to the class:
@api recordId;
@track props;
@track errorMsg;
@wire(findProperties, {
recordId: '$recordId',
priceRange: '100000'
})
props
<isExposed>
to true.Add a label, description, and target after the <isExposed>
tag:
<masterLabel>Similar Properties</masterLabel>
<description>This component searches for similar properties.</description>
<targets>
<target>lightning__RecordPage</target>
</targets>
Click Save in App Builder, and then Back.
The page should now display a list of properties in the same price range.
similarProperty
and save it in the default location.api
from lwc
.@api item;
to the class definition.{item.Name}
on line 2 of similarProperty.html.Go back to the similarProperties.html, and replace {item.Name}
inside the <li>
with:
<c-similar-Property item={item}></c-similar-Property>
Refresh the Property Detail page.
You shouldn’t notice any change.
Replace {item.Name}
in similarProperty.html with:
<lightning-record-view-form object-api-name="Property__c"
record-id={item.Id}>
<lightning-output-field field-name="Name"></lightning-output-field>
</lightning-record-view-form>
Save the file and push to your scratch org, then refresh the Property Detail page.
Add the following to the SimilarProperty.cmp after the initial lightning:outputField
:
<lightning-output-field field-name="Beds__c"></lightning-output-field>
<lightning-output-field field-name="Baths__c"></lightning-output-field>
<lightning-output-field field-name="Price__c"></lightning-output-field>
<lightning-output-field field-name="Status__c"></lightning-output-field>
Save the file and push to your scratch org, then refresh the Property Detail page.
Replace the four lines of code you just pasted with the following:
<lightning-layout multiple-rows>
<lightning-layout-item size="6">
<lightning-output-field field-name="Price__c"></lightning-output-field>
</lightning-layout-item>
<lightning-layout-item size="6">
<lightning-output-field field-name="Beds__c"></lightning-output-field>
</lightning-layout-item>
<lightning-layout-item size="6">
<lightning-output-field field-name="Baths__c"></lightning-output-field>
</lightning-layout-item>
<lightning-layout-item size="6">
<lightning-output-field field-name="Status__c"></lightning-output-field>
</lightning-layout-item>
<lightning-layout-item size="12">
<lightning-output-field field-name="Broker__c"></lightning-output-field>
</lightning-layout-item>
</lightning-layout>
Save the file and push to your scratch org, then refresh the Property Detail page.
Replace the contents of the <template>
with the following:
<div class="slds-media">
<div class="slds-media__figure">
<img src={item.Thumbnail__c} class="slds-avatar_large slds-avatar_circle" alt={item.Title_c} />
</div>
<div class="slds-media__body">
<div class="slds-grid slds-hint-parent">
<a onclick={navigateToRecord}>
<h3 class="slds-text-heading_small slds-m-bottom_xx-small">{item.Name}</h3>
</a>
</div>
<lightning-record-view-form object-api-name="Property__c"
record-id={item.Id}>
<lightning-layout multiple-rows>
<lightning-layout-item size="6">
<lightning-output-field field-name="Price__c"></lightning-output-field>
</lightning-layout-item>
<lightning-layout-item size="6">
<lightning-output-field field-name="Beds__c"></lightning-output-field>
</lightning-layout-item>
<lightning-layout-item size="6">
<lightning-output-field field-name="Baths__c"></lightning-output-field>
</lightning-layout-item>
<lightning-layout-item size="6">
<lightning-output-field field-name="Status__c"></lightning-output-field>
</lightning-layout-item>
<lightning-layout-item size="12">
<lightning-output-field field-name="Broker__c"></lightning-output-field>
</lightning-layout-item>
</lightning-layout>
</lightning-record-view-form>
</div>
</div>
Save the file and refresh the Property Detail page.
Something’s not quite right?
Check the code from your components against the following:
public with sharing class SimilarPropertyController {
@AuraEnabled(cacheable=true)
public static List<Property__c> findProperties (Id recordId, Decimal priceRange) {
List<Property__c> property = [SELECT Price__c FROM Property__c WHERE Id=:recordId];
Decimal propertyPrice = property[0].Price__c;
List<Property__c> similarProperties = getSimilarProperties(recordId, priceRange, propertyPrice);
return similarProperties;
}
private static List<Property__c> getSimilarProperties (Id recordId, Decimal priceRange, Decimal price ) {
Decimal range;
if (priceRange == null) {
range = 100000;
} else {
range = priceRange;
}
return [
SELECT Id, Name, Beds__c, Baths__c, Price__c, Broker__c, Status__c, Thumbnail__c
FROM Property__c WHERE Id != :recordId AND Price__c > :price - range AND Price__c < :price + range
];
}
}
<template>
<lightning-card title="Similar Properties"
icon-name="custom:custom85">
<div class="slds-m-around_medium">
<ul class="slds-list_vertical slds-has-dividers_top-space">
<template for:each={props.data} for:item="item" if:true={props.data}>
<li key={item.Id} class="slds-list__item">
<c-sim-prop-child theitem={item}></c-sim-prop-child>
</li>
</template>
<template if:true={props.error}>
<li class="slds-list__item"><h3 class="slds-text-heading_small slds-text-color_error">{props.error}</h3></li>
</template>
</ul>
</div>
</lightning-card>
</template>
import { LightningElement,api,track,wire } from 'lwc';
import findProperties from '@salesforce/apex/SimilarPropertyController.findProperties';
export default class SimilarProperties extends LightningElement {
@api recordId;
@track props;
@track errorMsg;
@wire(findProperties, {
recordId: '$recordId',
priceRange: '100000'
})
props
}
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="similarProperties">
<apiVersion>45.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Similar Properties</masterLabel>
<description>This component searches for similar properties.</description>
<targets>
<target>lightning__RecordPage</target>
</targets>
</LightningComponentBundle>
<template>
<div class="slds-media">
<div class="slds-media__figure">
<img src={item.Thumbnail__c} class="slds-avatar_large slds-avatar_circle" alt={item.Title_c} />
</div>
<div class="slds-media__body">
<div class="slds-grid slds-hint-parent">
<a onclick={navigateToRecord}>
<h3 class="slds-text-heading_small slds-m-bottom_xx-small">{item.Name}</h3>
</a>
</div>
<lightning-record-view-form object-api-name="Property__c"
record-id={item.Id}>
<lightning-layout multiple-rows>
<lightning-layout-item size="6">
<lightning-output-field field-name="Price__c"></lightning-output-field>
</lightning-layout-item>
<lightning-layout-item size="6">
<lightning-output-field field-name="Beds__c"></lightning-output-field>
</lightning-layout-item>
<lightning-layout-item size="6">
<lightning-output-field field-name="Baths__c"></lightning-output-field>
</lightning-layout-item>
<lightning-layout-item size="6">
<lightning-output-field field-name="Status__c"></lightning-output-field>
</lightning-layout-item>
<lightning-layout-item size="12">
<lightning-output-field field-name="Broker__c"></lightning-output-field>
</lightning-layout-item>
</lightning-layout>
</lightning-record-view-form>
</div>
</div>
</template>
import { LightningElement,api } from 'lwc';
export default class SimilarProperty extends LightningElement {
@api item;
}