Last active
May 19, 2020 20:34
-
-
Save mhamzas/2d3b775320d3262c2938c3e2db635301 to your computer and use it in GitHub Desktop.
Calling Apex method at regular interval from Lightning Component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" controller="RefreshContactCounter" access="global" > | |
| <aura:attribute name="recordId" type="Id"></aura:attribute> | |
| <aura:attribute name="pollId" type="String"></aura:attribute> | |
| <lightning:button variant="brand" label="Subscribe" title="Subscribe" onclick="{!c.init}" /> | |
| <lightning:button variant="brand-outline" label="Stop" title="Stop" onclick="{!c.cancelSubscribe}" /> | |
| </aura:component> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ({ | |
| checkSubscribeCall: function(component, event, helper){ | |
| //execute callApexMethod() again after 5 sec each | |
| var pollId = window.setInterval( | |
| $A.getCallback(function() { | |
| // Calling Apex Method | |
| helper.callApexMethod(component,helper,pollId); | |
| }), 5000 | |
| ); | |
| }, | |
| handleResponse : function (response, component){ | |
| var retVal = response.getReturnValue() ; | |
| // Your Logic | |
| }, | |
| callApexMethod : function (component,helper,pollId){ | |
| // Setting current pollId to stop the loop | |
| component.set('v.pollId', pollId); | |
| // Setting Apex Method | |
| var action = component.get("c.getContactCount"); | |
| // Setting Apex Param | |
| action.setParams({ Id : component.get("v.recordId") }); | |
| // Call Back Action | |
| action.setCallback(this, function(response) { | |
| this.handleResponse(response, component); | |
| }); | |
| $A.enqueueAction(action); | |
| } | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ({ | |
| init : function(component, event, helper) { | |
| // Calling Helper | |
| helper.checkSubscribeCall(component, event,helper); | |
| }, | |
| cancelSubscribe : function (component, event, helper){ | |
| window.clearInterval(component.get('v.pollId')); | |
| } | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class RefreshContactCounter { | |
| @AuraEnabled | |
| public static Integer getContactCount(){ | |
| AggregateResult[] groupedResults = [SELECT count(Id) totalCount FROM Contact]; | |
| Object totalCount = groupedResults[0].get('totalCount'); | |
| return (Integer) totalCount ; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment