function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
annette.hamilton1.3947453971418005E12annette.hamilton1.3947453971418005E12 

Live Agent Pre-Chat Survey

I've implemented Live Agent and have been utilized code from http://analyticsinthewild.blogspot.com/2014/03/extending-salesforce-liveagent-pre-chat.html to develop a Pre-Chat Survey that can do all of the following:
  • The preChat form capture first name, last name, and email (all required) and defaults the Agent greeting to include the provided first name (and Agent Name).
  • The preChat form searches for a matching Contact and a matching Lead with some specific conditions based on the outcome:
    • If a matching contact is found, DO NOT create a new Lead.
    • If a matching contact AND lead are found, bring up both.
    • If NO matching contact is found, do NOT create a new contact, but DO create a new Lead (as long as a matching Lead is not found).
Works like a charm but I also would like it to:
  • Allow the user to select a deployment based on Agent Skill button Sales or Service
  • If Service is required include information for a Case and show that case tab
  • If Sales is required do the steps outlined at the top of this thread.
I have it mostly working but am unable to get it ti create the Case...

Here is the code:

<apex:page showHeader="false" standardController="Account" extensions="preChatRemoting_Con">

<!-- This script takes the endpoint URL parameter passed from the deployment page and makes it the action for the form -->

<script type="text/javascript">
     (function() {
     function handlePageLoad() {
       var endpointMatcher = new RegExp("[\\?\\&]endpoint=([^&#]*)");
       document.getElementById('prechatForm').setAttribute('action',
       decodeURIComponent(endpointMatcher.exec(document.location.search)[1]));
     } if (window.addEventListener) {
              window.addEventListener('load', handlePageLoad, false);
   } else { window.attachEvent('onload', handlePageLoad, false);
              }})();

  function SubmitForm(createLead) {

      if (!createLead) {  //We found a matching contact based on email provided, so DO NOT send parameters to create a new lead.
          document.getElementById("optionA").value="";
          document.getElementById("optionB").value="false";
      }
      else {   //No matching contact was found, so send parameters required to create a new lead.
          document.getElementById("optionA").value="FirstName,true;LastName,true;Company,true;Email,true;Description,true;Status;true;Type,true;LeadSource,true;Source_Type__c,true;";
          document.getElementById("optionB").value="true";
      }
      document.getElementById("prechatForm").submit();
  }

  function getRemoteContact()
    {
        var contactEmail = document.getElementById('contactEmail').value;
        Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.preChatRemoting_Con.getcontact}', contactEmail, function(result, event){
                if (event.status) {
                    SubmitForm(false);  //contact found, don't create a lead
                } else if (event.type === 'exception') {
                    SubmitForm(true);  //contact NOT found, DO create a lead
                } else {
                    SubmitForm(false);  //unknown error, DON'T create a lead
                }
            },
            {escape: true}
        );
    }
</script>

<style>
body
{
    background-color:#f0eff7;
}
#chatFormDiv
{
    color:#1d2675;
    font-family:Arial, Helvetica;
    font-size:12px;
    font-weight:bold;
    line-height:20px;
    width:275px;
    font-weight:bold;
    text-align:left;
    padding:10px;

}
#chatHeader
{
    color:#1d2675;
    font-family:Arial, Helvetica;
    font-size:14px;
    font-weight:bold;
    line-height:18px;
    padding-bottom:10px;
}
label
{
    color:#1d2675;
    font-family:Arial, Helvetica;
    font-size:10px;
    font-weight:bold;
    line-height:12px;
    width:275px;
    font-weight:bold;
}
input[type=text], textarea
{
    width:275px;
    background: #f0eff7; /* Old browsers */
    background: -moz-linear-gradient(top, #f0eff7 0%, #ffffff 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f0eff7), color-stop(100%,#ffffff)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, #f0eff7 0%,#ffffff 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, #f0eff7 0%,#ffffff 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, #f0eff7 0%,#ffffff 100%); /* IE10+ */
    background: linear-gradient(to bottom, #f0eff7 0%,#ffffff 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f0eff7', endColorstr='#ffffff',GradientType=0 ); /* IE6-9 */   
    border-color: #dedede;
    border-top-color: #d3d3d3;
}
textarea
{
    height:140px;
}
.chatStatusDiv
{
    display:none;
}
</style>

<div id="chatFormDiv">
    <img src="https://sandbox-modspace.cs1.force.com/liveagent/resource/1398881220000/LiveAgentModSpaceLiveChat" /> <br/>
    <hr />
<div id="chatHeader">
    To help us serve you better, please provide the following information:
    </div>
    <form method="post" id="prechatForm">

<!-- Detail inputs -->
First Name: <input type="text" name="liveagent.prechat:leadFirstName" onchange="javascript: document.getElementById('prechat_field').value=this.value;" required="required"/><br />
Last Name: <input type="text" name="liveagent.prechat:leadLastName"  required="required"/><br />
Company: <input type="text" id="contactCompany" name="liveagent.prechat:leadCompany"  required="required"/><br />
Email: <input type="text" id="contactEmail" name="liveagent.prechat:leadEmail"  required="required"/><br />
Phone: <input type="text" id="contactPhone" name="liveagent.prechat:leadPhone"  required="required"/><br />
    <hr />
    Who would you like to chat with?<br />
          <!-- Values are LiveChatButton IDs. -->
          <input type="checkbox" name='liveagent.prechat.buttons' value='573S00000004CAz' />Sales - Quotes, Deliveries, Information<br />
Provide some details:<br />
Unit #: <input type="text" name="liveagent.prechat:caseAsset_ID_s" /><br />
Lease ID: <input type="text" name="liveagent.prechat:caseLease_ID" /><br />      
    <hr />
              <input type="checkbox" name='liveagent.prechat.buttons' value='573S00000004CBJ' />Service - Repairs, Billing, General<br />
Provide some details:<br />
Unit #: <input type="text" name="liveagent.prechat:caseAsset_ID_s" /><br />
Lease ID: <input type="text" name="liveagent.prechat:caseLease_ID" /><br />
    <hr />
    Additional Comments: <textarea name="liveagent.prechat:Body" id="body" required="required"></textarea><br />

<!--greeting field, copies from FirstName input-->
<input type="hidden" name="liveagent.prechat.name"  id='prechat_field'/>

<!--hidden fields written to the new lead-->
<input type="hidden" name="liveagent.prechat:leadStatus" value="Open" />
<input type="hidden" name="liveagent.prechat:leadType" value="Prospect" />
<input type="hidden" name="liveagent.prechat:leadSource" value="2021 - Live Chat" />
<input type="hidden" name="liveagent.prechat:leadSourceType" value="Inbound Chat" />

<!-- Creates an auto-query for a matching Contact record’s Email field based on the value of the liveagent.prechat:leadEmail field -->
    <input type="hidden" name="liveagent.prechat.query:leadEmail" value="Contact,Contact.Email" />


<!-- Map the detail inputs to the Lead fields -->
<input type="hidden" name="liveagent.prechat.findorcreate.map:Lead" value="FirstName,leadFirstName;LastName,leadLastName;Company,leadCompany;Email,leadEmail;Phone,leadPhone;Status;leadStatus;Type,leadType;LeadSource,leadSource;Source_Type__c,leadSourceType;" />

<!-- Map the detail inputs to the Contact fields -->
<input type="hidden" name="liveagent.prechat.findorcreate.map:Contact" value="FirstName,leadFirstName;LastName,leadLastName;Email,leadEmail;" />


<!-- Try to find Contact by email (exact match) -->
<input type="hidden" name="liveagent.prechat.findorcreate.map.doFind:Contact" value="Email,true;" />
<input type="hidden" name="liveagent.prechat.findorcreate.map.isExactMatch:Contact" value="Email,true;" />


<!-- Try to find the Lead by email (exact match) -->
<input type="hidden" name="liveagent.prechat.findorcreate.map.doFind:Lead" value="Email,true;" />
<input type="hidden" name="liveagent.prechat.findorcreate.map.isExactMatch:Lead" value="Email,true;" />

<!-- If the Lead is not found, then create one with the following fields set -->
<input type="hidden" id="optionA" name="liveagent.prechat.findorcreate.map.doCreate:Lead" value="FirstName,true;LastName,true;Company,true;Email,true;Description,true;Status;true;Type,true;LeadSource,true;Source_Type__c,true;" />

<!-- Save the Lead on the Live Chat Transcript -->
<input type="hidden" name="liveagent.prechat.findorcreate.saveToTranscript:Lead" value="Lead" />

<!-- Show the Lead when it is found or created -->
<input type="hidden" id="optionB" name="liveagent.prechat.findorcreate.showOnCreate:Lead" value="true" />

<!-- Show the Contact when it is found or created -->
<input type="hidden" name="liveagent.prechat.findorcreate.showOnCreate:Contact" value="true" />

<!-- Create a Case every time -->
<input type="hidden" name="liveagent.prechat:caseOrigin" value="Chat" />
<input type="hidden" name="liveagent.prechat.findorcreate.map:Case" value="Origin,caseOrigin;Subject,caseSubject;Asset_ID_s__c,caseAsset_ID_s;Lease_ID__c,caseLease_ID;" />
<input type="hidden" name="liveagent.prechat.findorcreate.map.doCreate:Case" value="Origin,true;Subject,true;Asset_ID_s__c,true;Lease_ID__c,true;" />
<input type="hidden" name="liveagent.prechat.findorcreate.saveToTranscript:Case" value="Case" />
<input type="hidden" name="liveagent.prechat.findorcreate.showOnCreate:Case" value="true" />

<!-- Link the Contact to the Case -->
<input type= "hidden" name="liveagent.prechat.findorcreate.linkToEntity:Contact" value="Case,ContactId" />

<!-- Display the customer detail to an agent in the Details tab  -->
<input type= "hidden" name= "liveagent.prechat.findorcreate.displayToAgent:String detailName" value= "Boolean display" />

<p>
<input type="button" value="Begin Chat Session" id="prechat_submit" onclick="javascript: getRemoteContact();"/></p>

</form>
</div>
</apex:page>

Any thoughts on how I can get this to work?

Thanks!!!