Salesforce Agentforce to Agent transfer Business Hours Check

Salesforce Agentforce to Agent transfer Business Hours Check

In this Blog Post, we are going to see how to handle Salesforce Agentforce to Human Agent transfer only during Business Hours.

Prerequisites:

Please do the following configuration steps shared in the following link:

1. Create an Apex Agent Action to check whether the request time is within the business hours or not.

Sample Apex Class:

public class BusinessHoursController {
    
    @InvocableMethod( label='Business Hours Check' )
    public static List < BusinessHoursOutput > checkBusinessHours(
        List < String > listRecordIds
    ) {
        
        if ( 
            listRecordIds != null &&
            listRecordIds.size() > 0 
        ) {
            
            System.debug(
                'Record Id::' +
                listRecordIds.get( 0 )
            );
            
        }
        
        BusinessHoursOutput objOutput = new BusinessHoursOutput();
        
        // Get the business hours
        Id BHId = [
            SELECT Id 
            FROM BusinessHours 
            WHERE Name = 'Default' 
            LIMIT 1
        ].Id;
        
        // Current date and time
        Datetime now = System.now();
        System.debug( 'now::' + now );
        
        // Check if fall within business hours
        Boolean isWithinBH = BusinessHours.isWithin(
            BHId, now
        );
        System.debug( isWithinBH );
        
        if ( !isWithinBH ) {
            
            objOutput.strMessage
                = 'I cannot transfer now. ' +
                'It is outside of Business Hours. ' +
                'For emergency, call our 24/7 1800';
            
        }
        
        objOutput.isWithinBusinessHours = isWithinBH;
        return new List < BusinessHoursOutput > { objOutput };
        
    }
    
    public class BusinessHoursOutput {
        
        @InvocableVariable( required=true )
        public String strMessage;
        @InvocableVariable( required=true )
        public Boolean isWithinBusinessHours;
            
    }
    
}

Apex Agent Action:

2. Create a new version of the standard Escalation Topic using the New Version button on the bottom right corner of the Topic. In the instruction, add additional prompt that it should transfer only if it is within business hours based on the custom Business Hours Check Agent Action.

Sample Instruction:

If a user explicitly asks to transfer to a live agent, escalate the conversation only if isWithinBusinessHours from Business Hours Check action is true. If it is false, share the  strMessage value from the action.

3. Add the custom Apex Action “Business Hours Check” to the Escalation Topic.

Output:

Leave a Reply