Skills-Based Routing Using Apex in Salesforce Omni-Channel

We had set up Salesforce Omni-Channel for one of our clients to route support cases using queue-based routing. Initially, we used flows to assign skills based on fixed values, such as product or language.

Later, the client requested a more dynamic routing setup, where the case should be assigned to agents based on multiple conditions like Customer Category, Language, and Case issue type.

For example, a case from a customer for a hardware issue in the German language should only go to an agent who meets all those criteria.

To meet this requirement, we reconfigured the routing using Apex instead of flows. In this Salesforce tutorial, I will explain how you can set up skills-based routing in Salesforce Omni Channel using Apex.

Skills-Based Routing Using Apex in Salesforce Omni-Channel

In the below example, we will create an Apex Class that will contain the method to assign cases to the agents based on their skills. We will set up skills-based routing from scratch in the steps below.

The prerequisite is that you should have enabled and set up Omni-Channel in your Salesforce org.

After this, you can proceed with the steps below to set up skills-based routing in Salesforce Omni Channel using Apex. We will cover the following points in this skill routing setup in Omni-Channel:

  • Enable Skill-Based Routing
  • Create a Service Channel
  • Create Skills
  • Create Service Resource for Agents
  • Create an Apex Class to Assign Work Based on Agent’s Skill
  • Create a Record-Trigger Flow to Call the Apex Class

Enable Skill-Based Routing in Salesforce Omni-Channel

The first step for skill-based routing is to enable it from the Omni-Channel settings.

To enable skills-based routing, navigate to Setup -> Omni-Channel Settings -> select the checkbox Enable Skills-Based and Direct-to-Agent Routing and click Save.

Skills based Routing in Omni-Channel using Apex

With this, the skills-based routing is enabled in the Omni-Channel.

Create a Service Channel

In this step, we will create a service channel. It allows us to manage sources of work and their priority compared to other work items. In this example, we will set up a service channel for the Case object to assign cases.

To create the service channel, navigate to Setup> Quick Find> Service Channels. In the Service Channel setup, click on the New button.

Create Skills Routing in Salesforce Omni Channel using Apex

Enter the Service Channel Name and the Developer Name. After this, select the object as ‘Case’ and click ‘Save‘.

Salesforce Omni-Channel Routing using Apex

This service channel will handle the routing of the case to the Agents.

Create Skills in Salesforce Omni-Channel

In this step, we will create skills in the Omni-Channel. These skills will be later assigned to the agents based on their expertise.

To create the skills, navigate to Setup > Quick Find > Skills. In the Skills Setup, click the New button.

Skill routing in Salesforce Omni Channel using Apex

Enter the Name and Label Name for the skill and save it. Here, don’t need to assign skills to the users and profiles. We will assign skills later in the Service Resources.

Skill based routing in Omni Channel using Apex

In the same way, you can create multiple skills that you need to assign to the agents in the routing.

Create Service Resource for Agents

Now, we will create service agents using the user accounts. After this, we will assign the created skills to the agents according to their expertise.

To create agents in the service resource, click on the App Launcher and select Service Resources.

Omni Channel Skill based routing using Apex

In the Service Resource, click on the New button and create the agent resource with the details below.

  • Enter the Name for the Agent, and keep it the same as the User Name.
  • Select the User that will be assigned as an agent in the routing configuration.
  • Select the Resource Type as Agent.
  • Select the Active checkbox and click Save.
Setup skill based routing using Apex in Omni Channel

In the same way, I have created other agents also. Here, a user can be associated with only one service resource. 

Now, to assign the skills to the agents, navigate back to the service resource and click on the Agent Resource name.

In the resource, go to the Details tab, and in the related list Service Resource Skill, click Assign Skills.

Assign Skills to agents in Omni Channel Salesforce

Select the checkbox to assign that specific skill to the user and then click Next.

Skills Routing in Omni Channel using Apex

In the same way, we can assign multiple skills to the agents.

Till we have set up skills and assigned those skills to agents. In the further steps, we will create an Apex class method to assign cases to the agents based on their skills.

Create an Apex Class to Assign Work Based on Agent’s Skill

Now, we will create an Apex class to assign work based on the agent’s skill. For this, navigate to the Salesforce developer console and follow the steps below.

  1. To create an Apex class, select File >New > Apex Class. After this, enter the name for the apex class and click OK.
Apex Class for skill routing in Salesforce Omni-Channel
  1. In this apex class, we will define the logic to route the cases. For that, create a public static void method routingCasesToAgents.

When the case is created, it should be forwarded to this method. For that, we will give an input parameter, which will be a list of strings.

public class SkillBasedRoutingDemoClass {
      public static void routingCasesToAgents(List<String> caseIds){
      }
}

In the further steps, we need to create a pending service request (PSR). After this, add skills to the request for the case, and finally, push the request to the queue.

  1. To create the pending service request, we need to have a list of cases. To get the cases, we will run a query on the cases.
public class SkillBasedRoutingClass {
    @InvocableMethod public static void routingCasesToAgents(List<String> caseIds){
        List<Case> casesInserted = [SELECT id, Language__c, Category from Case where ID in: caseIds];
        List<Skill> allSkillsinDB = [SELECT id, MasterLabel from Skill];
        }
}

This will retrieve the cases that need to be routed and fetch all available Skills.

  1. Now, have to loop through all cases that got inserted. Inside the loop, we need to create a pending service request for each of the cases. For this, we will use the pending routing service object.
    for(Case caseRec : casesInserted){
            PendingServiceRouting psr = new PendingServiceRouting();
            psr.workItemId = caseRec.Id;
            psr.RoutingModel = 'Most Available'; 
            psr.RoutingPriority = 1;
            psr.ServiceChannelId = '0N9J300000000ay'; 
            psr.IsReadyForRouting = FALSE;
            Insert psr;
            System.debug('success');

In the psr, we have mapped the work item ID to the Case record ID. By selecting Routing Model as ‘Most available‘, the case will be assigned to the agent that have the least work.

The Routing priority is one, and here I’ve hardcoded the service channel ID. IsReadyForRouting is initially false until skill mapping is complete.

  1. Now, we will add the skills to the request that we have created. For this, we need to fetch all the created skills. Add the code below under the method where we have fetched cases.
public class SkillBasedRoutingClass {
    @InvocableMethod public static void routingCasesToAgents(List<String> caseIds){
        List<Case> casesInserted = [SELECT id, Language__c, Category  from Case where ID in: caseIds];
        List<Skill> allSkillsinDB = [SELECT id, MasterLabel from Skill];
        }
}
  1. After fetching the skills, we will map the case field with the skills.
List<String> matchingSkillIds = new List <String>();
            For(Skill skillRec: allSkillsinDB){
                if ((caseRec.Language__c != null && caseRec.Language__c.contains(skillRec.MasterLabel)) ||
                    (caseRec.Issue_Type__c != null && caseRec.Issue_Type__c.contains(skillRec.MasterLabel))) {
                    matchingSkillIds.add(skillRec.Id);
                 }
            }

It will map the skills with the values from the field values in the case object. For example, if Language__c = “German”, then it finds the skill with MasterLabel = “German“.

  1. To add the required skill for the PendingServiceRouting record, add the code below.
List<SkillRequirement> skillReqsToInsert = new List<SkillRequirement>();
for(String matchingSkillId : matchingSkillIds) {
    SkillRequirement skillRequ = new SkillRequirement();
    skillRequ.SkillId = matchingSkillId;
    skillRequ.RelatedRecordId = psr.Id;
    skillRequ.SkillLevel = 10;
    skillReqsToInsert.add(skillRequ);
}
insert skillReqsToInsert;

It will add a required skill for the PendingServiceRouting record.

  1. Finally, complete the routing by setting the IsReadyForRouting flag to True.
psr.IsReadyForRouting = TRUE;
            Update PSR;

At last, save the Apex class. With this, we have defined the Apex class method that will assign the case to the agents based on the skills assigned to them.

Create a Trigger to Call the Apex Method

After creating the Apex class method, we need to define a trigger to call the Apex class. For this, you can either create an Apex trigger or a Record-triggered flow.

In this example, I will create a record-triggered flow. Follow the steps to create a record trigger flow that will call the apex class.

  1. Navigate to setup > Flows > click the New button.
  2. From the options, select Record-triggered flow and click Create.
Salesforce Omni-Channel skill routing using Apex
  1. In the trigger configuration, select the Case object. In the section “Trigger the Flow When“, select the option A record is created.

In the conditions requirements, I have selected none because we have to trigger the flow every time a case is created.

Omni channel routing in Salesforce using Apex
  1. To store the case IDs, we need to create a variable to hold the collection. To do this, go to the Manager tab in the Flow Builder and click ‘New Resource‘.

Define the new resource with the details below.

  • Select the Resource Type as Variable.
  • Enter the API Name as caseIdsCollection.
  • Select the Data Type as Text.
  • Select the checkbox Allow multiple values (collection) and click Done.
Setup Routing in Salesforce Omni Channel using Apex
  1. To assign the case IDs, add an Assignment element to the flow. In the section Set Variable Values, select the Variable as caseIdsCollection (collection variable).

Then select the Operator as Add, and in the Value, select Triggerring Case > Case ID.

Route Skills in Salesforce Omni Channel using Apex
  1. To call the Apex class, add the Action element to the flow. In the Action search for Apex, then select the Apex Class that we have created.
Skill based routing in Salesforce Omni Channel using Apex
  1. At last, save and activate the Record-triggered flow.

Test the Apex Skill-Based Routing in Salesforce Omni Channel

To test the case assignment in the Omni-Channel, follow the steps below.

  1. On the setup, click on the App Launcher and select Service Console.
  2. In the Service Console, ensure that Omni-channel is logged in and the status is Online.
Apex Method to Route skills in Salesforce Omni Channel
  1. Create a new case record. Enter the field values that you have mapped in the Apex code. In this example, I have mapped the fields Language_c and Need Support.
Use Apex class for skill based routing in Salesforce
  1. After that, change the case owner from the current user to the queue that is assigned to the Case object’s Service Channel.
Assign work in Salesforce Omni Channel using Apex

As we push the case to the queue, the case will be assigned to the user whose skills match the mapped field values.

Skill routing in Salesforce Omni Channel via Apex

This way, we can set up skills-based routing in Salesforce Omni-Channel using the Apex class method.

I hope by following the above steps, you understood how to build a custom skills-based routing setup in Omni-Channel using Apex.

By using Apex, we were able to route cases based on multiple criteria like language, customer category, and issue type, ensuring that the most suitable agent handles each case.

This method gives you more control compared to static flow configurations, especially in scenarios where routing logic depends on multiple field values or changing conditions.

You may also like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.