Lightning App Background Utility Items

In this blog, I am going to show how to use the background utility items run without a visible entry in the utility bar. background utility items provide an unobtrusive way to add functionality to your Lightning apps without cluttering the user experience. In standard navigation and console navigation apps, you can create and add background utility items to invisibly execute code. Implement the lightning:backgroundUtilityItem interface to use a custom component as a background utility item. In this example, I am logging the details into the custom object whenever the user landing into the application for audit purpose. here is the simple custom object that will be used to store the audit history

Apex Class

public class AuditMonitoringService {
    public static void insertAudit(String recordId){
        Audit_History__c aHistory = new Audit_History__c() ; 
        aHistory.View_By__c = UserInfo.getUserName() ;
        aHistory.Application_Name__c='Lightning Service';
        insert aHistory ;

Lightning Component 

<aura:component implements="lightning:backgroundUtilityItem,force:hasRecordId" controller="AuditMonitoringService">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    doInit : function(component, event, helper) {
        var action = component.get("c.insertAudit");
        action.setCallback(this, function(response) {
            var state = response.getState();
            // Display toast message to indicate load status
            var toastEvent = $A.get("e.force:showToast");
            if (state === 'SUCCESS'){
                    "title": "Success!",
                    "message": " Audit Log is created ."
            else {
                    "title": "Error!",
                    "message": " Something has gone wrong."

Add Component to  Utility Bar 

  1. Navigate to Setup > App Manager > New Lightning App
  2. Edit the Lightning  App > Under the App Settings >Utility Items > Add Utility item as shown below. Save it 


After adding the background utility item to the app whenever the user landed on the application it will log into the custom object and shows the notification message as shown below.