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
Vijay NagarathinamVijay Nagarathinam 

Live agent issue


I am using a live agent and display the below fields. If the person clicks the chat now button, I want to create a new lead with the given details. If the person using the same email to chat again in this case we need to update the details.

Please let me know how to achieve this.

User-added image

Amit Singh 1Amit Singh 1
Hello Vijay,

Use below code for pre-chat page and make the modification as per your requirement.
<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=([^&#]*)");
     } 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.
      else {   //No matching contact was found, so send parameters required to create a new lead.

  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}

<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=""  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="" 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="" value="FirstName,leadFirstName;LastName,leadLastName;Email,leadEmail;" />

<!-- Try to find Contact by email (exact match) -->
<input type="hidden" name="" value="Email,true;" />
<input type="hidden" name="" value="Email,true;" />

<!-- Try to find the Lead by email (exact match) -->
<input type="hidden" name="" value="Email,true;" />
<input type="hidden" name="" value="Email,true;" />

<!-- If the Lead is not found, then create one with the following fields set -->
<input type="hidden" id="optionA" name="" 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();"/>

Also, visit the below link.

Hope this helps.

Vijay NagarathinamVijay Nagarathinam
I am getting error to save the above code in my org