0

Is there any way to dynamically pass values to liveagent.addCustomDetail method?

When I use document.getElementById('email').value instead of text in the customdetail method the Chat button disappears and when I replace it with text it reappears.

<script type='text/javascript'>
    liveagent.CustomDetail('Contact E-mail', '[email protected]');
    //how to dynamically pass email value from form
</script>
<form>

  <!-- Detail inputs -->
  <p>First Name:</p><input type="text" id="firstName" /><br />
  <p>Last Name:</p><input type="text"  id="lastName" /><br />
  <p>Email:</p><input type="text" id="email" /><br />

</form>
3
  • Are you using pre-chat form?
    – Saroj Bera
    Commented Jun 24, 2016 at 4:09
  • @SarojBera I dont want to use a prechat form, I am planning to display a regular html form with a chat button in that page and send values from that form to Agent Console. As I have mentioned above, I dont want to hard-code values but I want to pull values dynamically and pass to agent console when he/she is available. liveagent.CustomDetail('Contact E-mail', '[email protected]'); -> this works liveagent.CustomDetail('Contact E-mail',document.getElementById('email').value ); -> this doesnt work
    – Sriram Lns
    Commented Jun 24, 2016 at 13:53
  • Please check my updated answer.
    – Saroj Bera
    Commented Jun 24, 2016 at 14:17

1 Answer 1

1

Yes you can do that. You have to update the Salesforce generated code as below. Only one issue user has to disable pop-up blocker.

Salesforce generated code.

<html>
    <head><title>AskNow LiveAgent Chat</title></head>
    <body>
        <div id="chat-icon">
            <img id="liveagent_button_online_57328000000L25v" style="display: none; border: 0px none; cursor: pointer" onclick="liveagent.startChat('57328000000L25v')" src="https://ap2.salesforce.com/resource/1455000961000/OnlineChatButton" />
            <img id="liveagent_button_offline_57328000000L25v" style="display: none; border: 0px none; " src="https://ap2.salesforce.com/resource/1455001009000/OfflineChatButton"/>
        </div>
        <script type='text/javascript' src='https://c.la10.salesforceliveagent.com/content/g/js/36.0/deployment.js'></script>
        <script type="text/javascript">
            function setName() {
                 document.getElementById("prechat_field").value = "Hellloooo";
                 return true;
            }
            liveagent.init('https://d.la10.salesforceliveagent.com/chat', '57228000000GpYe', '00D28000001KSvw');
            if (!window._laq) { window._laq = []; }
            window._laq.push(function(){
                liveagent.enableLogging();
                liveagent.setChatWindowHeight('720');
                liveagent.showWhenOnline('57328000000L25v', document.getElementById('liveagent_button_online_57328000000L25v'));
                liveagent.showWhenOffline('57328000000L25v', document.getElementById('liveagent_button_offline_57328000000L25v'));
            });
        </script>
    </body>
</html>

Updated code

<html>
    <head><title>AskNow LiveAgent Chat</title></head>
    <body>
        <div id="chat-icon">
            <form>
              <!-- Detail inputs -->
              <p>First Name:</p><input type="text" id="firstName" /><br />
              <p>Last Name:</p><input type="text"  id="lastName" /><br />
              <p>Email:</p><input type="text" id="email" /><br />
              <input type="button" value="Submit" onClick="startChat();return false;">
            </form>         
        </div>
        <script type='text/javascript' src='https://c.la10.salesforceliveagent.com/content/g/js/36.0/deployment.js'></script>
        <script type="text/javascript">
            function setName() {
                 document.getElementById("prechat_field").value = "Hellloooo";
                 return true;
            }
            function startChat(){
                liveagent.init('https://d.la10.salesforceliveagent.com/chat', '57228000000GpYe', '00D28000001KSvw');
                if (!window._laq) { window._laq = []; }
                window._laq.push(function(){
                    liveagent.enableLogging();
                    liveagent.setChatWindowHeight('720');
                    liveagent.showWhenOnline('57328000000L25v', document.getElementById('liveagent_button_online_57328000000L25v'));
                    liveagent.showWhenOffline('57328000000L25v', document.getElementById('liveagent_button_offline_57328000000L25v'));
                    liveagent.addCustomDetail('First Name', document.getElementById('firstName').value);
                    liveagent.addCustomDetail('Last Name', document.getElementById('lastName').value);
                    liveagent.addCustomDetail('Email', document.getElementById('email').value);
                    liveagent.addButtonEventHandler('57328000000L25v',function (e){
                        if(e == liveagent.BUTTON_EVENT.BUTTON_AVAILABLE){              
                            liveagent.startChat('57328000000L25v');
                        }       
                    });
                });

            }
        </script>
    </body>
</html>
3
  • Thanks for the code, this code works very well but there is a problem - two chat windows pop on submit and after 50 seconds two more windows pop up. This continues until the Agent goes offline or the user refreshes the page. Why does it occur and how do we resolve it? Thanks again, I am pretty new to live agent implementation and unable to find useful docs.
    – Sriram Lns
    Commented Jun 24, 2016 at 15:51
  • @SriramLns did you resolve this? I'm having the same issue as you described in your comment after using this code.
    – Json
    Commented Dec 23, 2016 at 20:07
  • @Dante I had to tweak the code a bit, I am passing the values in the URL where the chat button is placed and then I am parsing the values from URL and using liveagent.addCustomDetail() I am able to send the values to pre-chat form.
    – Sriram Lns
    Commented Jan 19, 2017 at 18:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .