[SalesForce] Dynamically Pass value to liveagent.addCustomDetail

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', 'jane@doe.com');
    //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>

Best Answer

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>
Related Topic