Skip to content

Instantly share code, notes, and snippets.

@F4bsi
Forked from JitendraZaa/AccountEdit.cmp.html
Last active August 2, 2019 14:39
Show Gist options
  • Select an option

  • Save F4bsi/6385281711481d2a7c221bf5e7b04975 to your computer and use it in GitHub Desktop.

Select an option

Save F4bsi/6385281711481d2a7c221bf5e7b04975 to your computer and use it in GitHub Desktop.
Salesforce Lightning view Counter Component which can be added to a Record Page to count the views on that record. The sObject needs the custom field "View_Count__c" as Number(18, 0).
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global">
<aura:attribute name="toUpdate" type="Object"
description="The record object to be displayed"/>
<aura:attribute name="toUpdateRecord" type="Object"
description="A simplified view record object to be displayed"/>
<aura:attribute name="recordSaveError" type="String"
description="An error message bound to force:recordData"/>
<aura:attribute name="viewCounted" type="Boolean" default="false"
description="Was the current view already added to the view Count?"/>
<force:recordData aura:id="toCountRec"
layoutType="FULL"
recordId="{!v.recordId}"
targetError="{!v.recordSaveError}"
targetRecord="{!v.toUpdate}"
targetFields="{!v.toUpdateRecord}"
mode="EDIT"
recordUpdated="{!c.recordUpdated}"/>
</aura:component>
<design:component label="Lightning View Counter">
</design:component>
Display the source blob
Display the rendered blob
Raw
<svg width="600" height="600" xmlns="http://www.w3.org/2000/svg">
<g>
<title>background</title>
<rect fill="none" id="canvas_background" height="602" width="602" y="-1" x="-1"/>
</g>
<g>
<title>Layer 1</title>
<rect rx="41" id="svg_4" height="575" width="563" y="12.500001" x="18.5" stroke-opacity="null" stroke-width="null" stroke="null" fill="#56aadf"/>
<path stroke="null" id="svg_3" fill="#ffffff" d="m532.12304,300.297534c0,0 -93.444286,161.294945 -232.123725,161.294945c-138.678059,0 -232.122345,-161.294945 -232.122345,-161.294945s93.444286,-161.890017 232.122345,-161.890017c138.68082,0 232.123725,161.890017 232.123725,161.890017zm-118.441461,0c0,-62.495008 -51.185875,-113.680884 -113.682264,-113.680884s-113.680884,51.185875 -113.680884,113.680884s51.184494,113.679503 113.680884,113.679503s113.682264,-51.185875 113.682264,-113.679503zm-113.682264,-51.185875c-27.973917,0 -50.590803,23.807031 -50.590803,51.185875c0,27.973917 22.616886,50.590803 50.590803,50.590803c27.378845,0 51.185875,-22.616886 51.185875,-50.590803c0,-27.378845 -23.807031,-51.185875 -51.185875,-51.185875z"/>
</g>
</svg>
({
savetoUpdate : function(component, event, helper) {
component.find("toCountRec").saveRecord($A.getCallback(function(saveResult) {
if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
console.log("Save completed successfully.");
} else if (saveResult.state === "INCOMPLETE") {
component.set("v.recordSaveError","User is offline, device doesn't support drafts.");
} else if (saveResult.state === "ERROR") {
var errMsg = "";
// saveResult.error is an array of errors,
// so collect all errors into one message
for (var i = 0; i < saveResult.error.length; i++) {
errMsg += saveResult.error[i].message + "\n";
}
component.set("v.recordSaveError", errMsg);
} else {
component.set("v.recordSaveError",'Unknown problem, state: ' + saveResult.state + ', error: ' +
JSON.stringify(saveResult.error));
}
}));
},
recordUpdated : function(component, event, helper) {
var changeType = event.getParams().changeType;
if (changeType === "CHANGED") {
component.find("toCountRec").reloadRecord();
} else if (changeType === "LOADED") {
let count = component.get('v.toUpdateRecord.View_Count__c');
component.set('v.toUpdateRecord.View_Count__c', count + 1);
if ( !component.get('v.viewCounted') ) {
component.set('v.viewCounted', true);
var a = component.get('c.savetoUpdate');
$A.enqueueAction(a);
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment