Fallback Routing Code Sample

Implement fallback routing rules in pre-chat forms so visitors can chat with another agent with the specified agent isn’t available.

The legacy chat product is scheduled for retirement on February 14, 2026, and is in maintenance mode until then. During this phase, you can continue to use chat, but we no longer recommend that you implement new chat channels. To avoid service interruptions to your customers, migrate to Messaging for In-App and Web before that date. Messaging offers many of the chat features that you love plus asynchronous conversations that can be picked back up at any time. Learn about chat retirement in Help.

Important

This sample creates a pre-chat form with fallback routing rules enabled. This form:

  • Requests a visitor’s name and email address.
  • Displays that information in the chat log and in the chat request window.
  • Displays either a new or existing Contact record with the customer’s information in a new tab in the Salesforce console. The customer’s name and email address are used to find an existing record. If no existing record is found, a new record is created and populated with the customer’s information.
  • Displays a drop-down list that lets visitors choose a different Chat button through which to route their chat request.
  • Routes chats directly to a specific agent, or, if that agent is unavailable, routes those chats based on the button’s default routing rules.
1<apex:page showHeader="false">
2
3<!-- This script takes the endpoint URL parameter passed from the deployment page
4    and makes it the action for the form -->
5<script type="text/javascript">
6 
7(function() {
8    function handlePageLoad() {
9        var endpointMatcher = new RegExp("[\\?\\&]endpoint=([^&#]*)");
10        var domainMatcher = new RegExp("^(https?:\\/\\/(.+?\\.)?(salesforce|salesforceliveagent)\\.com(\\/[A-Za-z0-9\\-\\._~:\\/\\?#\[\\]@!$&'\\(\\)\*\\+,;\\=]*)?)");
11
12        var endpointAttr = endpointMatcher.exec(document.location.search)[1];
13        // if the endpoint domain is valid
14        if (domainMatcher.test(decodeURIComponent(endpointAttr))) {
15            document.getElementById('prechatForm').setAttribute('action',
16                decodeURIComponent(endpointAttr.replace("javascript:", "")));
17        } else {
18            // invalid endpoint domain, set the action to empty
19            console.error("invalid domain: " + endpointAttr);
20            document.getElementById('prechatForm').setAttribute('action', "");
21        }
22    }
23    if (window.addEventListener) {
24        window.addEventListener('load', handlePageLoad, false);
25    } else {
26        window.attachEvent('onload', handlePageLoad, false);
27    }
28})();
29</script>
30
31<h1>Pre-chat Form</h1> 
32<form method='post' id='prechatForm'> 
33      Name: <input type='text' name='liveagent.prechat.name' id='prechat_field' /><br /> 
34      Email Address: <input type='text' name='liveagent.prechat:Email' /><br /> 
35      Department: <select name="liveagent.prechat.buttons"> 
36          <!-- Values are LiveChatButton and/or User IDs. -->
37          <option value="573D01234567890">Route through button 573D01234567890</option>
38          <option value="005D01234567890">Route to agent 005D01234567890</option>
39          <option value="005D01234567890_573D01234567890">Route to agent 005D01234567890 
40          with Fallback to button 573D01234567890</option> 
41      </select><br /> 
42      <input type='submit' value='Request Chat' id='prechat_submit'/> 
43</form>
44
45</apex:page>