December 4, 2020

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.

Agent Id(User Id) to route the work.

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 -

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' );


  1. If you also create the skill requirement for this record, then will it be assign to preferred agent or the skill resources?

    1. If PreferredUserId is specified and IsPreferredUserRequired is set to true, it will go to that specific user directly. If IsPreferredUserRequired is set to false and if that user is offline, it will go and find other Agents with the skills.