November 2, 2021

How to create Case from Einstein BOT only when Chat is transferred to an agent and change the owner in Salesforce using OmniToolKit?

1. Create the following Apex class.

Apex Controller:
public class ChatAcceptedController {
    
    @AuraEnabled
    public static void updateCase( String strChatTranscriptId, String strUserId ) {
        
        LiveChatTranscript objChatTranscript = [ SELECT Id, CaseId FROM LiveChatTranscript WHERE Id =: strChatTranscriptId ];
        
        if ( objChatTranscript != null ) {
            
            Case objCase = [ SELECT Id, OwnerId FROM Case WHERE Id =: objChatTranscript.CaseId ];
            
            if ( objCase != null ) {
                
                objCase.OwnerId = strUserId;
                update objCase;
                
            }
            
        }
    }

}


2. Create an Aura Component to call apex when the Chat is accepted by an agent. The apex class will change the owner of the Case record created from the Einstein Bot when the Chat is transferred to an agent from the BOT.

Aura Component:
<aura:component implements="flexipage:availableForAllPageTypes" access="global" controller="ChatAcceptedController">
    <aura:attribute name="acceptedBool" type="Boolean" default="false"/>
    <lightning:omniToolkitAPI aura:id="omniToolkit" />  
    <aura:handler event="lightning:omniChannelWorkAccepted" action="{! c.onWorkAccepted }"/>
</aura:component>

Aura JavaScript Controller:
({
    
    onWorkAccepted : function( component, event, helper ) {
        
        let checkBool = component.get( "v.acceptedBool" );
        
        if ( !checkBool ) {
            
            component.set( "v.acceptedBool", true );
            console.log( "Work accepted." );
            let workItemId = event.getParam( 'workItemId' );
            let workId = event.getParam( 'workId' );
            let userId = $A.get( "$SObjectType.CurrentUser.Id" );
            console.log( workItemId );
            console.log( workId );
            console.log( userId );
            if ( workItemId.startsWith( "570" ) ) {
                
                let action = component.get( "c.updateCase" );  
                action.setParams({  
                    strChatTranscriptId: workItemId,
                    strUserId: userId
                });  
                action.setCallback(this, function(response) {  
                    let state = response.getState();  
                    if ( state === "SUCCESS" ) {  
                        
                        $A.get('e.force:refreshView').fire();   
                        
                    }  else {
                        
                        let showToast = $A.get( "e.force:showToast" );
                        showToast.setParams({
                            title : 'Testing Toast!!!',
                            message : 'Case Record Owner not updated due to some error(s).' ,
                            type : 'error',
                            mode : 'sticky'
                        });
                        showToast.fire();
                        
                    }
                });  
                $A.enqueueAction( action );  
                
            }
            
        }
        
    }
    
})


3. Create a Flow to create Case record. The flow will be called from the BOT when the Chat is transferred to the agent.







4. Add the Lightning Aura Component to the App.

 
Note:
Start automatically should be enabled so that the Aura Component will load when the app loads to start receiving notifications for work acceptance.
 
5. Call the flow from BOT from Transfer to Agent Dialog.
 

Output:
 
 
 

No comments:

Post a Comment