• Patrick Kloiber
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
I searched all over and finally found an example of how to code the Live Agent PreChat Form to use conditional logic to decide to pull up an existing contact or to create a new lead.  Here is the workflow:

Visitor comes to website and starts chat, we have them enter their email address on the PreChat Form, we take this email address and Search Salesforce for:

Does an Existing Contact exist if so, bring up contact in the SF Chat Console for the Agent
If an Existing Contact does not exist, then search and pull up existing lead or create a new Lead and bring up the lead in the SF Chat Console for the Agent.

Here is the website link: https://salesforce.stackexchange.com/questions/121055/create-lead-if-contact-does-not-exist-live-agent
Submitted by CodePirate (thank you for this submission, I wanted to post in this forum should others need this info)

Here is the code snippit:
<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;Status;true;LeadSource,true;RecordTypeId,true;Entity__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>

<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 />
Email: <input type="text" id="contactEmail" name="liveagent.prechat:leadEmail"  required="required"/><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:leadSource" value="Live Chat" />
<input type="hidden" name="liveagent.prechat:leadCompany" value="NA" />
<input type="hidden" name="liveagent.prechat:leadEntity" value="London Retail" />
<input type="hidden" name="liveagent.prechat:leadRT" value="01220000000VCTT" />

<!-- 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;Status;leadStatus;LeadSource,leadSource;RecordTypeId,leadRT;Entity__c,leadEntity;" />

<!-- 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;Status;true;LeadSource,true;RecordTypeId,true;Entity__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" />

<input type="button" value="Begin Chat Session" id="prechat_submit" onclick="javascript: getRemoteContact();"/>
</form>
</apex:page>
I searched all over and finally found an example of how to code the Live Agent PreChat Form to use conditional logic to decide to pull up an existing contact or to create a new lead.  Here is the workflow:

Visitor comes to website and starts chat, we have them enter their email address on the PreChat Form, we take this email address and Search Salesforce for:

Does an Existing Contact exist if so, bring up contact in the SF Chat Console for the Agent
If an Existing Contact does not exist, then search and pull up existing lead or create a new Lead and bring up the lead in the SF Chat Console for the Agent.

Here is the website link: https://salesforce.stackexchange.com/questions/121055/create-lead-if-contact-does-not-exist-live-agent
Submitted by CodePirate (thank you for this submission, I wanted to post in this forum should others need this info)

Here is the code snippit:
<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;Status;true;LeadSource,true;RecordTypeId,true;Entity__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>

<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 />
Email: <input type="text" id="contactEmail" name="liveagent.prechat:leadEmail"  required="required"/><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:leadSource" value="Live Chat" />
<input type="hidden" name="liveagent.prechat:leadCompany" value="NA" />
<input type="hidden" name="liveagent.prechat:leadEntity" value="London Retail" />
<input type="hidden" name="liveagent.prechat:leadRT" value="01220000000VCTT" />

<!-- 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;Status;leadStatus;LeadSource,leadSource;RecordTypeId,leadRT;Entity__c,leadEntity;" />

<!-- 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;Status;true;LeadSource,true;RecordTypeId,true;Entity__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" />

<input type="button" value="Begin Chat Session" id="prechat_submit" onclick="javascript: getRemoteContact();"/>
</form>
</apex:page>
Here is my form code. I'm trying to create a lead record from the pre-chat form if one does not already exist.

<form method='post' id='prechatForm'>
    First Name:<font color="red">*</font><input type="text" name="liveagent.prechat:FirstName" /><br />
    Last Name:<font color="red">*</font> <input type="text" name="liveagent.prechat:LastName" /><br />
    Email:<font color="red">*</font> <input type="text" name="liveagent.prechat:Email" /><br />
    Phone Number:<font color="red">*</font> <input type='text' name='liveagent.prechat:Phone' /><br />
    Company Name:<font color="red">*</font> <input type='text' name='liveagent.prechat:Company' /><br />
    State:<font color="red">*</font> <select name="liveagent.prechat:State">
          <option value='Select State' selected='selected'>Select State</option>
          <option value='AK'>AK</option>
          <option value='AL'>AL</option>
          <option value='AR'>AR</option>
          <option value='AZ'>AZ</option><option value='CA'>CA</option><option value='CO'>CO</option><option value='CT'>CT</option><option value='DE'>DE</option><option value='FL'>FL</option><option value='GA'>GA</option><option value='HI'>HI</option><option value='IA'>IA</option><option value='ID'>ID</option><option value='IL'>IL</option><option value='IN'>IN</option><option value='KS'>KS</option><option value='KY'>KY</option><option value='LA'>LA</option><option value='MA'>MA</option><option value='MD'>MD</option><option value='ME'>ME</option><option value='MI'>MI</option><option value='MN'>MN</option><option value='MO'>MO</option><option value='MS'>MS</option><option value='MT'>MT</option><option value='NC'>NC</option><option value='ND'>ND</option><option value='NE'>NE</option><option value='NH'>NH</option><option value='NJ'>NJ</option><option value='NM'>NM</option><option value='NV'>NV</option><option value='NY'>NY</option><option value='OH'>OH</option><option value='OK'>OK</option><option value='OR'>OR</option><option value='PA'>PA</option><option value='RI'>RI</option><option value='SC'>SC</option><option value='SD'>SD</option><option value='TN'>TN</option><option value='TX'>TX</option><option value='UT'>UT</option><option value='VA'>VA</option><option value='VT'>VT</option><option value='WA'>WA</option><option value='WI'>WI</option><option value='WV'>WV</option><option value='WY'>WY</option>
        </select><br />
        <br/><font color="red">*</font>Indicates a required field.<br /><br />
        <!-- Saves values entered in the pre-chat fields to the chat transcript -->
        <input type="hidden" name="liveagent.prechat.save:FirstName" value="First_Name__c" />
        <input type="hidden" name="liveagent.prechat.save:LastName" value="Last_Name__c" />
        <input type="hidden" name="liveagent.prechat.save:Email" value="Email__c" />
        <input type="hidden" name="liveagent.prechat.save:Phone" value="Phone__c" />
        <input type="hidden" name="liveagent.prechat.save:Company" value="Company__c" />
        <input type="hidden" name="liveagent.prechat.save:State" value="State__c" />
       
<!-- Map lead fields  -->
<input type="hidden" name="liveagent.prechat.findorcreate.map:Lead" value="FirstName,FirstName;LastName,LastName;Email,Email;" />

<!-- Match lead by email -->
<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;" />

<!-- Create unmatched lead -->
<input type="hidden" name="liveagent.prechat.findorcreate.map.doCreate:Lead" value="FirstName,true;LastName,true;Email,true;" />

<!-- Save lead to lookup -->
<input type="hidden" name="liveagent.prechat.findorcreate.saveToTranscript:Lead" value="Lead" />

<!-- Display lead -->
<input type="hidden" name="liveagent.prechat.findorcreate.showOnCreate:Lead" value="true" />

    <input type='submit' value='Request Chat' id='prechat_submit'/> 
</form>
  • February 11, 2014
  • Like
  • 1