Add the following attribute:
<aura:attribute name="property" type="Property__c" />
Add a force:recordData
object to the component after the last aura:handler
:
<force:recordData aura:id="propertyService"
recordId="{!v.recordId}"
targetRecord="{!v.property}"
recordUpdated="{!c.doInit}"
layoutType="FULL" />
Add the following on a new line after the existing aura:handler
:
<aura:handler event="c:recordUpdated" action="{!c.doInit}" />
On a new line after the aura:attribute
add:
<aura:registerEvent name="recordUpdated" type="c:recordUpdated"/>
Add the following to the handleSuccess function, immediately after toastEvent.fire()
:
var recUpdate = $A.get("e.c:recordUpdated");
recUpdate.fire();
Add two new attributes to the SimilarProperties.cmp:
<aura:attribute name="searchCriteria" type="String" default="Price" />
<aura:attribute name="priceRange" type="String" default="100000" />
Update the title of the lightning:card
to:
{! 'Similar Properties by ' + v.searchCriteria}
var action = component.get("c.getSimilarProperties");
.Update the parameters for the action to:
action.setParams({
recordId: component.get("v.recordId"),
beds: component.get("v.property.fields.Beds__c.value"),
price: component.get("v.property.fields.Price__c.value"),
searchCriteria: component.get("v.searchCriteria"),
priceRange: parseInt(component.get("v.priceRange"), 10)
});
Remove the FindProperties method and update the getSimilarProperties method to match:
@AuraEnabled
public static List<Property__c> getSimilarProperties (Id recordId, String searchCriteria, Decimal beds, Decimal price, Decimal priceRange ) {
if (searchCriteria == 'Bedrooms') {
return [
SELECT Id, Name, Beds__c, Baths__c, Price__c, Broker__c, Status__c, Thumbnail__c
FROM Property__c WHERE Id != :recordId AND Beds__c = :beds
];
} else {
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
];
}
}
Add the following to the design file:
<design:attribute name="searchCriteria" label="Search By" datasource="Bedrooms, Price" default="Price" description="Search for similar houses based on what criteria?" />
<design:attribute name="priceRange" label="Price Range" default="100000" description="When searching by Price, search using the price plus or minus this amount" />
(Optional) Switch back to the Developer Console and add the following to the Design file:
<sfdc:objects >
<sfdc:object >Property__c</sfdc:object>
</sfdc:objects>
<path>
tag from the SVG.<path>
tag with the one you just copied.<path>
you just pasted, add fill=”#fff” before the “d” attribute.(Informational Only) Change width="120px" height="120px" viewBox="0 0 120 120"
in the <svg>
tag to:
width="100px" height="100px" viewBox="0 0 100 100"
<path>
to #F26891
.Select all and replace the .svg file in the Dev Console with:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="100px" height="100px" viewBox="0 0 100 100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<path d="M120,108 C120,114.6 114.6,120 108,120 L12,120 C5.4,120 0,114.6 0,108 L0,12 C0,5.4 5.4,0 12,0 L108,0 C114.6,0 120,5.4 120,12 L120,108 L120,108 Z" id="Shape" fill="#2A739E"/>
<path fill="#FFF" d="m78 24h-50v-2c0-1.1-0.9-2-2-2h-4c-1.1 0-2 0.9-2 2v56c0 1.1 0.9 2 2 2h4c1.1 0 2-0.9 2-2v-46h50c1.1 0 2-0.9 2-2v-4c0-1.1-0.9-2-2-2z m-4 14h-34c-3.3 0-6 2.7-6 6v22c0 3.3 2.7 6 6 6h34c3.3 0 6-2.7 6-6v-22c0-3.3-2.7-6-6-6z m-5.5 17h-2.5v10c0 0.6-0.4 1-1 1h-4c-0.6 0-1-0.4-1-1v-6c0-0.6-0.4-1-1-1h-4c-0.6 0-1 0.4-1 1v6c0 0.6-0.4 1-1 1h-4c-0.6 0-1-0.4-1-1v-10h-2.5c-0.5 0-0.7-0.6-0.3-0.9l11.2-10.9c0.4-0.3 0.9-0.3 1.3 0l11.2 10.9c0.3 0.3 0.1 0.9-0.4 0.9z"></path>
</g>
</svg>
Add a new line at the bottom of the markup before the closing </lightning:card>
and add the following to the new line:
<lightning:spinner aura:id="spinner" variant="brand" size="large"/>
Save the file.
Add class="slds-is-relative"
to the <lightning:card>
tag so that it looks like this:
<lightning:card iconName="custom:custom85" title="{! 'Similar Properties by ' + v.searchCriteria}" class="slds-is-relative">
Click STYLE to add a custom CSS file to the SimilarProperties component and replace the default contents with:
.THIS {
min-height: 13rem;
}
Add the following to the beginning of the doInit function before the var action
declaration:
var spinner = component.find("spinner");
$A.util.removeClass(spinner, "slds-hide");
Add the following at the end of the action’s callback:
$A.util.addClass(spinner, "slds-hide");
Replace the contents of the <ul>
with the following:
<aura:if isTrue="{!v.similarProperties.length > 0}">
<aura:iteration items="{!v.similarProperties}" var="item">
<li class="slds-list__item">
<c:SimilarProperty property="{!item}" />
</li>
</aura:iteration>
<aura:set attribute="else">
<li class="slds-list__item">
<h3 class="slds-text-small slds-text-color_error">No similar properties found.</h3>
</li>
</aura:set>
</aura:if>
Need a last second rescue? Check your code against the following:
<aura:component controller="MyPropertyController" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="recordId" type="Id" />
<aura:attribute name="similarProperties" type="Object[]" />
<aura:attribute name="property" type="Property__c" />
<aura:attribute name="searchCriteria" type="String" default="Price" />
<aura:attribute name="priceRange" type="String" default="100000" />
<aura:handler event="c:RecordUpdated" action="{!c.doInit}" />
<aura:handler name="init" value="{! this}" action="{!c.doInit}" />
<force:recordData aura:id="propertyService"
recordId="{!v.recordId}"
targetRecord="{!v.property}"
recordUpdated="{!c.doInit}"
layoutType="FULL" />
<lightning:card iconName="custom:custom85" title="{! 'Similar Properties by ' + v.searchCriteria}" class="slds-is-relative">
<div class="slds-p-left_medium slds-p-right_medium">
<ul class="slds-list_vertical slds-has-dividers_top-space">
<aura:if isTrue="{!v.similarProperties.length > 0}">
<aura:iteration items="{!v.similarProperties}" var="item">
<li class="slds-list__item">
<c:SimilarProperty property="{!item}" />
</li>
</aura:iteration>
<aura:set attribute="else">
<li class="slds-list__item">
<h3 class="slds-text-small slds-text-color_error">No similar properties found.</h3>
</li>
</aura:set>
</aura:if>
</ul>
</div>
<lightning:spinner aura:id="spinner" variant="brand" size="large"/>
</lightning:card>
</aura:component>
({
doInit : function(component, event, helper) {
var spinner = component.find("spinner");
$A.util.removeClass(spinner, "slds-hide");
var action = component.get("c.getSimilarProperties");
action.setParams({
recordId: component.get("v.recordId"),
beds: component.get("v.property.fields.Beds__c.value"),
price: component.get("v.property.fields.Price__c.value"),
searchCriteria: component.get("v.searchCriteria"),
priceRange: parseInt(component.get("v.priceRange"), 10)
});
action.setCallback(this, function(response){
var similarProperties = response.getReturnValue();
component.set("v.similarProperties", similarProperties);
console.log("f: ", component.get("v.similarProperties"));
$A.util.addClass(spinner, "slds-hide");
});
$A.enqueueAction(action);
}
})
.THIS {
min-height: 13rem;
}
<design:component >
<sfdc:objects >
<sfdc:object >Property__c</sfdc:object>
</sfdc:objects>
<design:attribute name="searchCriteria" label="Search By" datasource="Bedrooms, Price" default="Price" description="Search for similar houses based on what criteria?" />
<design:attribute name="priceRange" label="Price Range" default="100000" description="When searching by Price, search using the price plus or minus this amount" />
</design:component>
<aura:component >
<aura:attribute name="property" type="Property__c" />
<aura:registerEvent name="recordUpdated" type="c:RecordUpdated"/>
<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:buttonIcon iconName="utility:edit" class="slds-col_bump-left" iconClass="slds-button__icon_hint" variant="bare" alternativeText="Edit Record" onclick="{!c.editRecord}" />
</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>
<lightning:recordEditForm aura:id="editForm" recordId="{!v.property.Id}" objectApiName="Property__c" class="slds-hide" onsuccess="{!c.handleSuccess}">
<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 >
<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:inputField fieldName="Beds__c"/>
</lightning:layoutItem>
<lightning:layoutItem size="6" >
<lightning:inputField fieldName="Baths__c" />
</lightning:layoutItem>
<lightning:layoutItem size="6" >
<lightning:inputField fieldName="Price__c" />
</lightning:layoutItem>
<lightning:layoutItem size="6" >
<lightning:inputField fieldName="Status__c" />
</lightning:layoutItem>
</lightning:layout>
<lightning:layout horizontalAlign="center" class="slds-m-top_large">
<lightning:button variant="neutral" label="Cancel" title="Cancel" type="text" onclick="{!c.handleCancel}" />
<lightning:button variant="brand" label="Submit" title="Submit" type="submit" />
</lightning:layout>
</div>
</div>
</lightning:recordEditForm>
</aura:component>
({
navToRecord : function (component, event, helper) {
var navEvt = $A.get("e.force:navigateToSObject");
navEvt.setParams({
"recordId": component.get("v.property.Id")
});
navEvt.fire();
},
editRecord : function(component, event, helper) {
helper.showHide(component);
},
handleSuccess : function(component, event, helper) {
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
"title": "Success!",
"message": "The property's info has been updated.",
"type": "success"
});
toastEvent.fire();
var recUpdate = $A.get("e.c:RecordUpdated");
recUpdate.fire();
helper.showHide(component);
},
handleCancel : function(component, event, helper) {
helper.showHide(component);
event.preventDefault();
}
})
public with sharing class MyPropertyController {
@AuraEnabled
public static List<Property__c> getSimilarProperties (Id recordId, String searchCriteria, Decimal beds, Decimal price, Decimal priceRange ) {
if (searchCriteria == 'Bedrooms') {
return [
SELECT Id, Name, Beds__c, Baths__c, Price__c, Broker__c, Status__c, Thumbnail__c
FROM Property__c WHERE Id != :recordId AND Beds__c = :beds
];
} else {
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
];
}
}
}