Lightning Console Set Tab Label Dynamically

Salesforce console is one of the key areas for agents to boost service productivity.we will see how to set the console case tab label dynamically based on the case origin.we will append the case origin to the case number while setting the label.

Apex Class 

public class ChangeCaseLabel {
    public static ChangeCaseLabelService service = new ChangeCaseLabelService();
    @AuraEnabled 
    public static String getCaseOrigin(String caseId){
        return service.getCaseOrigin(caseId);
    }
    
    
}

Service Class 

public class ChangeCaseLabelService {
    
    public String getCaseOrigin(String caseId){
        Case c = [Select id ,CaseNumber , Origin from Case where Id = :caseId];
        return c.Origin+'-'+c.CaseNumber ; 
    }
}

Lightning Component 

<aura:component implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" controller="ChangeCaseLabel">
    <aura:handler name="init" value="this" action="{!c.doInit}"/>
    <aura:handler event="force:refreshView" action="{!c.doInit}" />  
    <lightning:workspaceAPI aura:id="workspace" />
</aura:component>
({
	doInit : function(component, event, helper) {
		helper.init(component, event, helper);
	}
})
({
    init : function(component, event, helper) {
        var action = component.get("c.getCaseOrigin");
        action.setParams({ caseId: component.get("v.recordId")});
        action.setCallback(this,$A.getCallback(function(response1)
         {
         var state = response1.getState();             
         if (state === "SUCCESS")  
         {
             var result= response1.getReturnValue();
         var workspaceAPI = component.find("workspace");
         workspaceAPI.getFocusedTabInfo().then(function(response) {
            var focusedTabId = response.tabId;
            workspaceAPI.setTabLabel({
                tabId: focusedTabId,
                label: result
            });
        })
        .catch(function(error) {
            console.log(error);
        });
         }
         }
        ));
        $A.enqueueAction(action);       
        
        
    }
})

The following line will return the workspace API library reference

 var workspaceAPI = component.find("workspace");

The following method of the workspace API will be used to set label.

workspaceAPI.setTabLabel({
                tabId: focusedTabId,
                label: result
            });

Now add this component to the Lightning record page as shown below.

Testing 

If the case is created form the web the case label is appended with the web and case number as shown below

If the case is created from the email, then case label will be changed to email – case number.