How to route the Work to the specific Agent using Omni-Channel in Salesforce?

How to route the Work to the specific Agent using Omni-Channel in Salesforce?

Create a Pending Service Routing record with the below fields to route the Work to the specific Agent using Omni-Channel.

PreferredUserId:
Agent Id(User Id) to route the work.

IsPreferredUserRequired:
Boolean to define whether the work waits for the agent when unavailable or falls back to the queue.
Indicates whether a work item should stay with the preferred user even when the user is not available, the default value is false.

For additional fields, check this – https://developer.salesforce.com/docs/atlas.en-us.omni_channel_dev.meta/omni_channel_dev/sforce_api_objects_pendingservicerouting.htm

Custom field in Case for example:
 

 

Sample Apex Class:
public class OmniChannelRoutingController {
    
    /*
        Primary method to route records through Omni-Channel
    */
    public static void routeToPreferredAgents( List < sObject > listRecords, String strEntity ) {
    
        List < PendingServiceRouting > listPSRs = new List < PendingServiceRouting >();
        String strChannelId = getChannelId( strEntity );
    
        for ( sObject obj : listRecords )        
            listPSRs.add( createPendingServiceRouting( obj, strChannelId ) );
        
        if ( listPSRs.size() > 0 ) {
        
            insert listPSRs;
            
        }
                
    }
    
    /*
        Method to create Pending Service Routing record
    */
    static PendingServiceRouting createPendingServiceRouting( sObject obj, String strChannelId ) {
    
        PendingServiceRouting psrObj = new PendingServiceRouting(
            CapacityWeight = 1,
            IsReadyForRouting = true,
            RoutingModel = ‘MostAvailable’,
            RoutingPriority = 1,
            ServiceChannelId = strChannelId,
            WorkItemId = obj.Id,
            PushTimeout = 0,
            RoutingType = ‘SkillsBased’,
            PreferredUserId = (String) obj.get( ‘Preferred_User__c’ ),
            IsPreferredUserRequired = true
        );
        return psrObj;
        
    }
    
    
    
    /*
        Method to get the Channel Id based on the Entity
    */
    static String getChannelId( String strEntity ) {
    
        ServiceChannel channel = [ SELECT Id From ServiceChannel Where RelatedEntity =: strEntity ];
        return channel.Id;
        
    }
    
}

 
Sample Code to call the method:
List < Case > listCases = [ SELECT Id, Preferred_User__c FROM Case WHERE Id = ‘5005w00001fqspyAAA’ ];
OmniChannelRoutingController.routeToPreferredAgents( listCases, ‘Case’ );

Leave a Reply