0

I am using lighting empApi to refresh the component whenever a case record is created. I have a trigger & apex class in case object that fires and update the platform event object.

In Detail-record page, where you can see Tracking component(one of the components in Detail-record page) whenever a new case is created it's retrieving the new data into tracking component, for that I am using lightning:empApi concept using platform event I capturing the live record without a manual refresh.

Now my issue is this auto refresh is working for all the records in the same object instead of one I am working on.

So far I am thinking that, after inserting the case record, the component reloads using empApi and calling the subscribe method. that time it's not checking with the recordId.

and also I am using show spinner in the components if the data is loading. So, I am not sure if this is the reason for the issue.

Please ask me if you have any question

  <aura:component controller="ClassController"
            implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId,force:hasSObjectName"
            access="global">

   <aura:attribute name="recordId" 
                type="Id" />

  <aura:handler name="sub"  value="{!this}" action="{!c.Subcribe}" />

<lightning:empApi aura:id="empApi"/>


          <aura:if isTrue="{!v.showSpinner}">
                    <lightning:spinner alternativeText="Loading" size="small"/>
                </aura:if>
        //// code

Controller

    var empApi = component.find('empApi')
    var replayId = -1
    var channel = component.get('v.channel')
    helper.getHelperfunction(component, event, helper)

    var sub = function(message){

        helper.getHelperfunction(component, event, helper)
    }

      empApi.subscribe(channel, replayId, sub)

Helper class

          ({
        getHelperfunction: function(component,event,helper) {
        component.set("v.showSpinner",true);
        var action = component.get('c.getListOfCases');
        action.setCallback(this,function(response){
        if(response.getState() === "SUCCESS"){
            component.set("v.listOfCases",response.getReturnValue());    
            component.set("v.showSpinner",false);
        } 
       });
      $A.enqueueAction(action);
}

})

Trigger

   trigger Cases on Case (after insert) {
if(Trigger.isAfter) {
    // Send the list of cases (Trigger.new) to the example function
    ExampleClass.publishCaseUpdateNotifications(Trigger.new);
}

}

class for fire from trigger

      public class ExampleClass {
      public static void publishCaseUpdateNotifications(List<Case> cases) {
      List<Case_Upd__e> notifications = new List<Case_Upd__e>();
      for (Case c: cases) {
        notifications.add(new Case_Upd__e(
            CaseNumber__c = c.CaseNumber
        ));
    }

    List<Database.SaveResult> results = EventBus.publish(notifications);
  • What does gHelperfunction() do? – David Reed Jun 23 at 0:01
  • I have an added gHelperfunction () code, simply get the callback function for subscribe method. – Stephen Roy Jun 23 at 12:06
0

Your code is performing lots of unnecessary server trips and refreshes because it doesn't

  1. Provide enough detail in the Platform Event, as fired by the trigger, for each subscriber to determine if the event is relevant to what it is displaying. Based on what I recall of your previous questions, that would probably be a parent record Id.
  2. Evaluate the Platform Event details in the JavaScript callback method to determine if it needs to perform a refresh at all - i.e., whether the event it's receiving actually affects the data it is displaying.

If you fix that issue, you may even obviate the need to perform a server round trip at all. Including more details in the Platform Event might allow your consumer to query the server only once, and thereafter update purely via events. Be aware, however, that adopting such an architecture does open up a race condition that you'll need to be ready to handle.

Lastly, calling your Platform Event Low_Ink__e, as copied and pasted directly from the documentation, is going to be confusing. You should name it something that reflects what it actually does.

  • How to Evaluate the Platform Event details in the JavaScript callback method. using this document lightning:empApi/documentation I can fetch the platform event using $A.getCallback but how to evaluate the platform event details into Js helperController. – Stephen Roy Jun 24 at 12:46
  • @StephenRoy Your function sub() throws away the message that the empApi component is providing. You would need to pass the message to your handler function and inspect the values in the message there. – David Reed Jun 24 at 12:48
  • If possible could you please provide any example how to pass the message to handler function. – Stephen Roy Jun 24 at 13:05
  • Stephen, I'm just saying that you need to pass the parameter you're already receiving to another function. Then, write code to check the values in the message you receive based on the changes you make to your Platform Event, as I suggest above. I don't have any prewritten examples that apply to that use case. – David Reed Jun 24 at 13:06
  • empApi.subscribe(channel, replayId, sub).then(function(newSubscription){ component.set('v.subscribeview', newSubscription) you meant like this. – Stephen Roy Jun 24 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.