Skip to content

Instantly share code, notes, and snippets.

View choudharymanish8585's full-sized avatar
💭
Never Settle

Manish Choudhari choudharymanish8585

💭
Never Settle
View GitHub Profile
//Run this code in anonymous window of developer console
//Here my channel name is "AccountSpy" which will be used for subscription
PushTopic pushTopic = new PushTopic();
pushTopic.Name = 'AccountSpy';
pushTopic.Query = 'SELECT Id, Name, Type, Phone, Website FROM Account';
pushTopic.ApiVersion = 43.0;
pushTopic.NotifyForOperationCreate = true;
pushTopic.NotifyForOperationUpdate = true;
pushTopic.NotifyForOperationUndelete = true;
pushTopic.NotifyForOperationDelete = true;
public class StreamingApiController {
/**
* This method returns signed in user's session id
* @author Manish Choudhari
* */
@AuraEnabled
public static String getSessionId() {
return UserInfo.getSessionId();
}
}
({
/**
* This function makes a handshake request to server using current user's session id
* Once the handshake is complete, susbcribe to the channel
* @author Manish Choudhari
* @version 1.0.0
* */
doInit: function(component, event, helper) {
//Call server action to get current user's session id
var action = component.get("c.getSessionId");
<aura:component access="global" controller="StreamingApiController">
<!-- ChannelName, which needs to subscribed -->
<aura:attribute name="channelName" type="String" required="true"/>
<!-- Save the reference of cometD, which will be used to disconnect the subscription -->
<aura:attribute name="cometd" type="Object"/>
<!-- Save the reference of current subscription, which can be unsubscribe later on -->
<aura:attribute name="subscription" type="Object"/>
<!-- This event is fired when a component is destroyed.
Handle this event if you need to do custom cleanup when a component is destroyed.-->
<aura:handler name="destroy" value="{!this}" action="{!c.unsubscribe}"/>
<aura:event type="COMPONENT" description="Capture streaming api event. Takes message object parameter">
<aura:attribute name="message" type="Object" />
</aura:event>
public class AccountController {
@AuraEnabled
public static List<Account> getAccounts(){
return [SELECT
Id, Name, Phone, Rating, My_Custom_Field__c, Active__c
FROM Account LIMIT 200];
}
}
({
getAllAccounts : function(component, helper) {
//Calling base component's helper method to call Aura Method
helper.callServer(component, "c.getAccounts",
function(response){
if(response){
component.set("v.data", response);
//Calling showToast method of Base component
helper.showToast({
"title": "SUCCESS",
({
doInit : function(component, event, helper) {
helper.getAllAccounts(component, helper);
},
})
<!--Exntends Base Component in Component Definition -->
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes"
extends="c:Base" controller="AccountController" access="global" >
<aura:attribute name="data" type="Account[]"/>
<aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
<aura:iteration items="{!v.data}" var="acc">
({
/*
* This method will call the server side action and will execute callback method
* it will also show error if generated any
* @param component (required) - Calling component
* @param method (required) - Server side methos name
* @param callback (required) - Callback function to be executed on server response
* @param params (optional) - parameter values to pass to server
* @param setStorable(optional) - if true, action response will be stored in cache
* */