[SalesForce] Link New Contact and Account from Live Agent Pre-Chat form sample

I am working with the Live Agent Developer Guide Pre-Chat Form code sample and have modified it to include a pre-chat form field for the Account name. I am able to use the sample code successfully to create the contact, case, and account and link the contact, case, and account to the live agent transcript and link the contact and account to the case with the supplied defaults.

Where I am having difficulty is for the condition when a match is not found by email and the form data is used to create a new contact/account, I am not having success linking the new contact to the new account that is created. I've tried modifying the findOrCreate.map and findorcreate.linkToEntity methods but have not had success. There is a note when using the findOrCreate.linkToEntity method that:

You can only link records if the parent record is created with a findOrCreate API call. You can’t link a child record to a record you found using the findOrCreate.linkToEntity method.

Even so, I am not certain this is the case in the below code sample.

Any suggestions to help link the new contact to the account would be awesome.

<apex:page showHeader="false">

    <!-- 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].replace("javascript:", "")));
} if (window.addEventListener) { 
window.addEventListener('load', handlePageLoad, false); 
} else { window.attachEvent('onload', handlePageLoad, false); 
}})(); 
</script>

<h1>Live Agent Pre-Chat Form</h1>

<!-- Form that gathers information from the chat visitor and sets the values to Live Agent Custom Details used later in the example -->
<form method='post' id='prechatForm'>
    First name: <input type='text' name='liveagent.prechat:ContactFirstName' id='firstName' /><br />
    Last name: <input type='text' name='liveagent.prechat:ContactLastName' id='lastName' /><br />
    District name: <input type='text' name='liveagent.prechat:ContactAccountName' id='accountName' /><br />
    Email: <input type='text' name='liveagent.prechat:ContactEmail' id='email' /><br />
    Phone: <input type='text' name='liveagent.prechat:ContactPhone' id='phone' /><br />
    Issue: <input type='text' name='liveagent.prechat:CaseSubject' id='subject' /><br />

<!-- Hidden fields used to set additional custom details -->
    <input type="hidden" name="liveagent.prechat:CaseStatus" value="New" /><br />

    <!-- This example assumes that "Chat" was added as picklist value to the Case Origin field -->
    <input type="hidden" name="liveagent.prechat:CaseOrigin" value="Chat" /><br />

    <!-- This example will set the Case Record Type to a specific value for the record type configured on the org. Lookup the case record type's id on your org and set it here -->
    <input type="hidden" name="liveagent.prechat:CaseRecordType" value="012D00123456789" />

    <!-- Used to set the visitor's name for the agent in the Console -->
    <input type="hidden" name="liveagent.prechat.name" id="prechat_field_name" />

<!-- map: Use the data from prechat form to map it to the Salesforce record's fields -->
<input type="hidden" name="liveagent.prechat.findorcreate.map:Account" value="Name,ContactAccountName" />
<input type="hidden" name="liveagent.prechat.findorcreate.map:Contact" value="FirstName,ContactFirstName;LastName,ContactLastName;Email,ContactEmail;Phone,ContactPhone" />
<input type="hidden" name="liveagent.prechat.findorcreate.map:Case" value="Subject,CaseSubject;Status,CaseStatus;Origin,CaseOrigin;RecordTypeId,CaseRecordType" />

<!-- doFind, doCreate and isExactMatch example for a Contact: 
    Find a contact whose Email exactly matches the value provided by the customer in the form 
    If there's no match, then create a Contact record and set it's First Name, Last Name, Email, and Phone to the values provided by the customer -->
<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" />
<input type="hidden" name="liveagent.prechat.findorcreate.map.doCreate:Account" value="Name,true" />
<input type="hidden" name="liveagent.prechat.findorcreate.map.doCreate:Contact" value="FirstName,true;LastName,true;Email,true;Phone,true;ContactAccountName,true" />

<!-- doCreate example for a Case: create a case to attach to the chat, set the Case Subject to the value provided by the customer and set the case's Status and Origin fields -->
<input type="hidden" name="liveagent.prechat.findorcreate.map.doCreate:Case" value="Subject,true;Status,true;Origin,true;RecordTypeId,true" />

<!-- linkToEntity: Set the record Contact record, found/created above, as the Contact on the Case that's created --> 
<input type="hidden" name="liveagent.prechat.findorcreate.linkToEntity:Account" value="Case,AccountId" />
<input type="hidden" name="liveagent.prechat.findorcreate.linkToEntity:Contact" value="Case,ContactId" />

<!-- showOnCreate: Open the Contact and Case records as sub-tabs to the chat for the agent in the Console -->
<input type="hidden" name="liveagent.prechat.findorcreate.showOnCreate:Contact" value="true" />
<input type="hidden" name="liveagent.prechat.findorcreate.showOnCreate:Case" value="true" />
<input type="hidden" name="liveagent.prechat.findorcreate.showOnCreate:Account" value="true" />

<!-- saveToTranscript: Associates the records found / created, i.e. Contact and Case, to the Live Chat Transcript record. --> 
<input type="hidden" name="liveagent.prechat.findorcreate.saveToTranscript:Contact" value="ContactId" />
<input type="hidden" name="liveagent.prechat.findorcreate.saveToTranscript:Account" value="AccountId" />
<input type="hidden" name="liveagent.prechat.findorcreate.saveToTranscript:Case" value="CaseId" />

<!-- displayToAgent: Hides the case record type from the agent -->
<input type="hidden" name="liveagent.prechat.findorcreate.displayToAgent:CaseRecordType" value="false" />

<!-- searchKnowledge: Searches knowledge article based on the text, this assumes that Knowledge is setup -->
<input type="hidden" name="liveagent.prechat.knowledgeSearch:CaseSubject" value="true" />

<input type='submit' value='Chat Now' id='prechat_submit' onclick="setName()"/>

<!-- Set the visitor's name for the agent in the Console to first and last name provided by the customer -->
<script type="text/javascript">
   function setName() {
    document.getElementById("prechat_field_name").value =  
        document.getElementById("firstName").value + " " + document.getElementById("lastName").value;
    }
</script>

<style type="text/css">
p {font-weight: bolder }
</style>

</form>

Best Answer

I solved my own question.

I simply appended a findOrCreate.linkToEntity method on the Account following the findOrCreate.map.doCreate:Contact method:

<input type="hidden" name="liveagent.prechat.findorcreate.map.doCreate:Contact" value="FirstName,true;LastName,true;Email,true;Phone,true;ContactAccountName,true" />
<input type="hidden" name="liveagent.prechat.findorcreate.linkToEntity:Account" value="Contact,AccountId" />
Related Topic