MyPropertyController
and then click OK.Replace the default code with:
public with sharing class MyPropertyController {
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
to the findProperties
method.SimilarProperties
, select Lightning Record Page, and then click Submit.controller="MyPropertyController"
to the <aura:component>
tag.Paste the following code into your new component inside the <aura:component>
tag:
<aura:attribute name="recordId" type="Id" />
<aura:attribute name="similarProperties" type="Object[]" />
<aura:handler name="init" value="{! this}" action="{!c.doInit}" />
<lightning:card iconName="custom:custom85" title="Similar Properties">
<div class="slds-p-left_medium slds-p-right_medium">
<ul class="slds-list_vertical slds-has-dividers_top-space">
<aura:iteration items="{!v.similarProperties}" var="item">
<li class="slds-list__item">
{!item.Name}
</li>
</aura:iteration>
</ul>
</div>
</lightning:card>
Replace the default code with the following:
({
doInit : function(component, event, helper) {
var action = component.get("c.findProperties");
action.setParams({
recordId: component.get("v.recordId"),
priceRange: "100000"
});
action.setCallback(this, function(response){
var similarProperties = response.getReturnValue();
component.set("v.similarProperties", similarProperties);
});
$A.enqueueAction(action);
}
})
Save the file.
Go back to a Property Record page, click the Setup icon and select Edit Page.
Locate the SimilarProperties component under Custom components and drag it onto the page at the top of the right-hand column.
Click Save and then Back.
The page should now display a list of properties in the same price range.
SimilarProperty
and leave all of the checkboxes unchecked.Add the following on line 2:
<aura:attribute name="property" type="Property__c" />
{!v.property.Name}
Go back to the SimilarProperties.cmp component. Replace {!item.Name}
inside the <li>
with:
<c:SimilarProperty property="{!item}" />
Save the file and refresh the Property Detail page.
You shouldn’t notice any change.
Replace {!v.property.Name}
in the SimilarProperty.cmp with:
<lightning:recordViewForm aura:id="viewForm" recordId="{!v.property.Id}" objectApiName="Property__c">
<lightning:outputField fieldName="Name" />
</lightning:recordViewForm>
Save the file and refresh the Property Detail page.
Add the following to the SimilarProperty.cmp after the initial lightning:outputField
:
<lightning:outputField fieldName="Beds__c"/>
<lightning:outputField fieldName="Baths__c"/>
<lightning:outputField fieldName="Price__c"/>
<lightning:outputField fieldName="Status__c"/>
Save the file and refresh the Property Detail page.
Replace the four lines of code you just pasted with the following:
<lightning:layout multipleRows="true" >
<lightning:layoutItem size="6" >
<lightning:outputField fieldName="Beds__c"/>
</lightning:layoutItem>
<lightning:layoutItem size="6" >
<lightning:outputField fieldName="Baths__c" />
</lightning:layoutItem>
<lightning:layoutItem size="6" >
<lightning:outputField fieldName="Price__c" />
</lightning:layoutItem>
<lightning:layoutItem size="6" >
<lightning:outputField fieldName="Status__c" />
</lightning:layoutItem>
</lightning:layout>
Save the file and refresh the Property Detail page.
Replace the contents of the <lightning:recordViewForm>
with the following:
<div class="slds-media">
<div class="slds-media__figure">
<img src="{!v.property.Thumbnail__c}" class="slds-avatar_large slds-avatar_circle" alt="{!v.targetFields.Title_c}" />
</div>
<div class="slds-media__body">
<lightning:layout class="slds-hint-parent">
<a onclick="{!c.navToRecord}">
<h3 class="slds-text-heading_small slds-m-bottom_xx-small">{!v.property.Name}</h3>
</a>
</lightning:layout>
<lightning:layout multipleRows="true" >
<lightning:layoutItem size="6" >
<lightning:outputField fieldName="Beds__c"/>
</lightning:layoutItem>
<lightning:layoutItem size="6" >
<lightning:outputField fieldName="Baths__c" />
</lightning:layoutItem>
<lightning:layoutItem size="6" >
<lightning:outputField fieldName="Price__c" />
</lightning:layoutItem>
<lightning:layoutItem size="6" >
<lightning:outputField fieldName="Status__c" />
</lightning:layoutItem>
</lightning:layout>
</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 MyPropertyController {
@AuraEnabled
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
];
}
}
<aura:component controller="myPropertyController" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="recordId" type="Id" />
<aura:attribute name="similarProperties" type="Object[]" />
<aura:handler name="init" value="{! this}" action="{!c.doInit}" />
<lightning:card iconName="custom:custom85" title="Similar Properties">
<div class="slds-p-left_medium slds-p-right_medium">
<ul class="slds-list_vertical slds-has-dividers_top-space">
<aura:iteration items="{!v.similarProperties}" var="item">
<li class="slds-list__item">
<c:SimilarProperty property="{!item}" />
</li>
</aura:iteration>
</ul>
</div>
</lightning:card>
</aura:component>
({
doInit : function(component, event, helper) {
var action = component.get("c.findProperties");
action.setParams({
recordId: component.get("v.recordId"),
priceRange: "100000"
});
action.setCallback(this, function(response){
var similarProperties = response.getReturnValue();
component.set("v.similarProperties", similarProperties);
});
$A.enqueueAction(action);
}
})
<aura:component >
<aura:attribute name="property" type="Property__c" />
<lightning:recordViewForm aura:id="viewForm" recordId="{!v.property.Id}" objectApiName="Property__c">
<div class="slds-media">
<div class="slds-media__figure">
<img src="{!v.property.Thumbnail__c}" class="slds-avatar_large slds-avatar_circle" alt="{!v.targetFields.Title_c}" />
</div>
<div class="slds-media__body">
<lightning:layout class="slds-hint-parent">
<a onclick="{!c.navToRecord}">
<h3 class="slds-text-heading_small slds-m-bottom_xx-small">{!v.property.Name}</h3>
</a>
</lightning:layout>
<lightning:layout multipleRows="true" >
<lightning:layoutItem size="6" >
<lightning:outputField fieldName="Beds__c"/>
</lightning:layoutItem>
<lightning:layoutItem size="6" >
<lightning:outputField fieldName="Baths__c" />
</lightning:layoutItem>
<lightning:layoutItem size="6" >
<lightning:outputField fieldName="Price__c" />
</lightning:layoutItem>
<lightning:layoutItem size="6" >
<lightning:outputField fieldName="Status__c" />
</lightning:layoutItem>
</lightning:layout>
</div>
</div>
</lightning:recordViewForm>
</aura:component>