0

I've been using JavaScript to check live agents availability for a while now and it was working fine until a recent change of requirements.

This is what I currently do:

if (!window._laq) { window._laq = []; }
window._laq.push(function(){
     liveagent.showWhenOnline('<Chat Button Id Here>', document.getElementById('<Online Element Id Here>'));
     liveagent.showWhenOffline('<Chat Button Id Here>', document.getElementById('<Offline Element Id Here>'));
});

However now I have a much more complex matrix of rules to handle. And I would like to be able to manage these rules from a Custom Setting, to make it more self-service-ish.

So I wonder if there's a way to check the agents availability from Apex instead of JavaScript, so I could have all my rules in a Visualforce page / component controller and route the user accordingly.

Thanks!

  • 1
    Pretty sure it can't be done. – Adrian Larson Apr 5 '18 at 17:11
  • Can you please explain a little bit more about the flow. How you are planning to use Apex here. – Saroj Bera Apr 6 '18 at 9:56
  • Hi @SarojBera. Imagine a form, with some inputs. Depending on the combination of these inputs from the user, and the combination of who is online or not, I will route the user to different Chat (ids). So, I was trying to see check the agents online during the form submission, in the apex controller. – Matheus Goncalves Apr 8 '18 at 2:16
0

Well, I couldn't find a way to check live agent availability from apex, but I was able combine different availabilities (part of my requirements) by showing different elements in the DOM and later, in the page load, checking which elements were visible or not, and displaying different buttons for each scenario:

If it helps someone out there, this is the solution:

Step 1: check who's available and show the < div > accordingly. These are the < div >:

<div id="support_is_online" style="display: none; color: rgba(0,0,0,0);"></div>
<div id="sales_is_online" style="display: none; color: rgba(0,0,0,0);"></div>
<div id="general_is_online" style="display: none; color: rgba(0,0,0,0);"></div>

And here the script to show it or hide it.

<script type="text/javascript">

    if (!window._laq) { window._laq = []; }
    window._laq.push(
    function () {

        liveagent.showWhenOnline('ID0000004545545', document.getElementById('support_is_online'));   // Based on Support Skill
        liveagent.showWhenOnline('ID0000004545544', document.getElementById('sales_is_online'));     // Based on Sales Skill
        liveagent.showWhenOnline('ID0000004545546', document.getElementById('general_is_online'));   // Based on General Skill

        liveagent.showWhenOnline('ID0000004545547', document.getElementById('sales_is_online'));     // Real Chat, for Sales Only
        liveagent.showWhenOnline('ID0000004545548', document.getElementById('support_is_online'));   // Real Chat, for Sales & Support
        liveagent.showWhenOnline('ID0000004545549', document.getElementById('general_is_online'));   // Real Chat, for Sales & General
        liveagent.showWhenOnline('ID0000004545550', document.getElementById('general_is_online'));   // Real Chat, for Sales, Support & General

    }   
);
</script>

So, the next step is, during the page load, check if these divs are visible or not, and show the right buttons accordingly:

DIVs:

<div id="chat_sales_online" style="display: none;">
    <a href="javascript://Chat" onclick="liveagent.startChat('ID0000004545547')">Start a Chat - SAL</a>
</div>

<div id="chat_sales_support_online" style="display: none;">
    <a href="javascript://Chat" onclick="liveagent.startChat('ID0000004545548')">Start a Chat - SAL_SUP</a>
</div>

<div id="chat_sales_general_online" style="display: none;">
    <a href="javascript://Chat" onclick="liveagent.startChat('ID0000004545549')">Start a Chat - SAL_GEN</a>
</div>

<div id="chat_sales_general_support_online" style="display: none;">
<a href="javascript://Chat" onclick="liveagent.startChat('ID0000004545550')">Start a Chat - SAL_GEN_SUP</a>
</div>

<div id="buttons_offline" style="display: none;">Live chat is currently unavailable, please try again later or Contact Us for more information.</div>

And here, the javascript to get the combination of agents availability:

<script type="text/javascript">

    // method to check if the element is hidden
    function isHidden(el) {
       return (el.offsetParent === null)
    }

    window.onload = function () {
        var onlineSUP = !isHidden(document.getElementById('support_is_online'));
        var onlineSAL = !isHidden(document.getElementById('sales_is_online'));
        var onlineGEN = !isHidden(document.getElementById('general_is_online'));

        var SAL_GEN_SUP = (onlineSUP && onlineSAL && onlineGEN);
        var SAL_GEN = (!onlineSUP && onlineSAL && onlineGEN);
        var SAL_SUP = (onlineSUP && onlineSAL && !onlineGEN);
        var SAL = (!onlineSUP && onlineSAL && !onlineGEN);
        var allOffline = (!onlineSUP && !onlineSAL && !onlineGEN);

        if(allOffline) {
            document.getElementById('buttons_offline').style.display = 'block';
        }else if(SAL_GEN_SUP) {
            document.getElementById('chat_sales_general_support_online').style.display = 'block';
        } else if (SAL_GEN) {
            document.getElementById('chat_sales_general_online').style.display = 'block';
        } else if (SAL_SUP) {
            document.getElementById('chat_sales_support_online').style.display = 'block';
        } else if (SAL) {
            document.getElementById('chat_sales_online').style.display = 'block';
        } else {
            document.getElementById('buttons_offline').style.display = 'block';
        }

        // Hide the elements used to check the availability

        document.getElementById('support_is_online').style.display = 'none';
        document.getElementById('sales_is_online').style.display = 'none';
        document.getElementById('general_is_online').style.display = 'none';
    }

</script>

This way I was able to, dinamically, display different buttons based on the combination of agents availability.

I guess Salesforce should enable us to get this information by using some methods inside apex classes, but, still, that would do the trick.

I hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.