openUtility on case change event

Use Streaming API to capture the events from the database.

Create a push topic

First, you need to create a push topic with a specific query, based on that query the event will be fired.

In your case, it will be something like.

Select Name,..., LastModifiedById FROM Case where Status = 'Closed'

As you need to fire this event only when the case is updated you can set NotifyForOperationUpdate to true on Push Topic record. Make sure that the IsActive is set to true.

So the final code to insert the push topic will be like below.

PushTopic pushTopic = new PushTopic();
pushTopic.Name = 'WhenCaseClosed';
pushTopic.Query = 'Select Name,..., LastModifiedById FROM Case where Status = \'Closed\'';
pushTopic.ApiVersion = 49.0;
pushTopic.NotifyForOperationUpdate = true;
pushTopic.NotifyForFields = 'Where'; // as you want to fire this event only when the status field is changed.
insert pushTopic;

Note that you just need a single push topic record in the org.

Use the Streaming API event listener to open the utility.

Add the lightning:empApi in your component and add the event handler in the init handler of the component. Note you will also need lightning:utilityBarAPI.

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

Now let's see the handler code.

const empApi = component.find('empApi');
const replayId = -1;
const channel = '/topic/WhenCaseClosed'; // note the same name we have set to the push topic.
// Subscribe to an event
    empApi
    .subscribe(
        channel,
        replayId,
        $A.getCallback(eventReceived => {
            // Process event (this is called each time we receive an event)
            if(eventReceived.data && eventReceived.data.sobject){
                // check if the last modifyby id of the current record is same as the logged in user.
                if(eventReceived.data.sobject.LastModifiedById === $A.get("$SObjectType.CurrentUser.Id")){
                    // open the utility here.
                }
            }
        })
    )
    .then(subscription => {});

As we had already added the LastModifiedById in the query, so we are checking if the LastModifiedById is the same as the logged-in user. So we can say that it is the same user who has closed the case.

I hope this will solve all of your problems.

Corner case.

There is a possibility that the LastModifiedById might get changed in between the case is closed and the event is received on the Utility component. To avoid that you can create a custom field to store the user id who has closed the case.

The Platform events

I have not tried but I think this can be also achieved with Platform Events as they also use Streaming Apis and EMP APIs. One advantage using PushTopic over Platform events I see is you don't need to fire the any events from the trigger like we do in Platform Events.


You should use lightning data services in the components. Lightning data services automatically captures records changes and you can get update details(like which field is updated and value of the field). After that you can utilityBarAPI to open the utility.

Code Markup - Cmp

<aura:component implements="force:hasRecordId,flexipage:availableForRecordHome">
<aura:attribute name="caseRecord" type="Object"/>
<aura:attribute name="recordLoadError" type="String"/>

<force:recordData aura:id="recordLoader"
                  recordId="{!v.recordId}"
                  layoutType="{!v.layout}"
                  fields="CaseNumber,Description,Subject,Status"
                  targetFields="{!v.caseRecord}"
                  targetError="{!v.recordLoadError}"
                  recordUpdated="{!c.recordUpdated}"
                  />
<!-- Display a lightning card with details about the case -->
<lightning:card iconName="standard:case" title="{!v.caseRecord.Name}" >
    <div class="slds-p-horizontal--small">
        <p class="slds-text-heading--small">
            <lightning:formattedPhone title="Phone"  value="{!v.caseRecord.CaseNumber}" /></p>
        <p class="slds-text-heading--small">
            <lightning:formattedText title="Description" value="{!v.caseRecord.Description}" /></p>
    </div>
</lightning:card></aura:component>

Component Controller js

({
recordUpdated: function(component, event, helper) {
    var eventParams = event.getParams();
    if(eventParams.changeType === "CHANGED") {
        var changedFields = eventParams.changedFields;
        let fields = Object.keys(changedFields);
        let values = Object.values(changedFields);
        for(let field of Object.keys(changedFields)){
            if(field === 'Status'){
                let value = changedFields[field]['value'];
                if(value === 'Closed'){
                    // Place your Open Utility Code here
                    console.log('Open Utility');
                }
            }
        }
    }
}

})

Refer to the Link:- Lightning Data Services - Record Changes