TransWikia.com

Create lead if contact does not exist live agent

Salesforce Asked by C0DEPirate on November 20, 2020

I was able to create lead if contact does not exist, but hidden fields are not getting mapped:

Can anybody advise on how to achieve this?

Below is Prechat page:

<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;";
          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>

2 Answers

Please note the reproduction code in this question is vulnerable. Please do not use it! Use the Official Pre-Chat Form Code Sample by Salesforce instead.

Answered by JohnSfdc on November 20, 2020

I found the solution of this problem.

Issue was related to mapping of fields in the JavaScript Changed the line :

document.getElementById("optionA").value="FirstName,true;LastName,true;Company,true;Email,true;Description,true;Status;true;Type,true;leadSource,true;";

to

document.getElementById("optionA").value="FirstName,true;LastName,true;Company,true;Email,true;Status;true;LeadSource,true;RecordTypeId,true;Entity__c,true;";

Answered by C0DEPirate on November 20, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP