Skip to content

Instantly share code, notes, and snippets.

@danieljpeter
Last active July 10, 2017 10:32
Show Gist options
  • Save danieljpeter/5985796 to your computer and use it in GitHub Desktop.
Save danieljpeter/5985796 to your computer and use it in GitHub Desktop.
This is a visualforce form for Live Agent for salesforce. It is a prechat form that does server-side input validation based on the Contact object. It checks to see if a contact exists for the given email. If it exists, it does nothing. If it doesn't exist, it creates the contact. Then when the customer comes in for chat, we can pop to their cont…
public class SupportPreChat {
public Contact theContact {get; set;}
public String endpoint {get; set;}
public String jsSubmitForm {get; set;}
public SupportPreChat() {
jsSubmitForm = '';
theContact = new Contact();
//get the querystring data passed in by the button. This is the liveagent form action
//EX: ?endpoint=https%3A%2F%2F1.la1a1.salesforceliveagent.com... etc..
endpoint = '';
PageReference pageRef = ApexPages.currentPage();
if (pageRef.getParameters() != null) {
if (pageRef.getParameters().containsKey('endpoint')) {
if (pageRef.getParameters().get('endpoint') != null) {
endpoint = pageRef.getParameters().get('endpoint');
}
}
}
}
public pagereference clickChat() {
PageReference pr = null;
Id AccountId = '001xxx'; //ID for an umbrella account to stick these contacts on
Id OwnerId = '005xxx'; //ID for a default user to set as the owner of the contact
//see if the email submitted exists as a contact
List<Contact> contactList = new List<Contact>();
if ( (theContact.email != null) && (theContact.email != '') ) {
contactList = [SELECT Id FROM Contact WHERE email=:theContact.email LIMIT 1];
if (contactList.isEmpty()) {
//create the contact
Contact c = new Contact(
FirstName = theContact.FirstName,
LastName = theContact.LastName,
Email = theContact.Email,
AccountId = AccountId,
OwnerId = OwnerId
);
insert c;
}
}
jsSubmitForm = 'jQuery(\'#prechatForm\').submit();';
return pr;
}
static testMethod void doTest() {
String testEndpoint = 'https%3A%2F%2F1.la1a1.salesforceliveagent.com';
PageReference pg = Page.SupportChatPreChatSafariGeneral;
Test.setCurrentPage(pg);
ApexPages.currentPage().getParameters().put('endpoint', testEndpoint);
SupportPreChat spc = new SupportPreChat();
spc.theContact.FirstName = 'John';
spc.theContact.LastName = 'Smith';
spc.theContact.Email = 'johnsmith@test.com';
spc.theContact.Description = 'This some text';
spc.clickChat();
}
}
<apex:page showHeader="false" cache="false" expires="0" standardstylesheets="false" controller="SupportPreChat" id="page">
<head>
<apex:includeScript value="{!URLFOR($Resource.jqueryValidation, 'lib/jquery-1.7.2.js')}"/>
<apex:outputPanel id="prechatJS">
<script>
jQuery.noConflict();
jQuery(function(){
{!jsSubmitForm}
});
</script>
</apex:outputPanel>
<style type="text/css">
body {
text-align: left;
}
input.prechat {
width: 320px;
}
</style>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="0" width="100%" style="width:100%; background-color:#0a5088;">
<tr>
<td align="left"><img src="{!$Resource.SupportChatSafariChatHeaderLeft}" /></td>
<td width="*">&nbsp;</td>
<td align="right"><img src="{!$Resource.SupportChatSafariChatHeaderRight}" /></td>
</tr>
</table>
<div style="padding: 5px;">
<apex:form id="prechatFormInput">
First Name
<br/>
<apex:inputField id="FirstName" value="{!theContact.FirstName}" required="true" styleClass="prechat" />
<br /><br />
Last Name
<br />
<apex:inputField id="LastName" value="{!theContact.LastName}" required="true" styleClass="prechat" />
<br /><br />
Username (Email Address)
<br />
<apex:inputField id="Email" value="{!theContact.Email}" required="true" styleClass="prechat" />
<br /><br />
Subject
<br />
<apex:inputTextarea id="Subject" value="{!theContact.Description}" rows="4" cols="50" styleClass="prechat"/>
<br />
<apex:commandButton reRender="prechatJS,prechatFormPanel,prechatFormInput" action="{!clickChat}" value="Request Chat" id="prechat_submit" />
</apex:form>
</div>
<apex:outputPanel id="prechatFormPanel">
<form method='post' id='prechatForm' action="{!endpoint}">
<input type="hidden" name='liveagent.prechat:FirstName' value="{!theContact.FirstName}"/>
<input type="hidden" name='liveagent.prechat:LastName' value="{!theContact.LastName}"/>
<input type="hidden" name='liveagent.prechat:Email' value="{!theContact.Email}"/>
<input type="hidden" name='liveagent.prechat:Subject' value="{!theContact.Description}"/>
<input type="hidden" name="liveagent.prechat.query:Email" value="Contact,Contact.Email" />
<input type="hidden" name="liveagent.prechat.save:FirstName" value="FirstName__c" />
<input type="hidden" name="liveagent.prechat.save:LastName" value="LastName__c" />
<input type="hidden" name="liveagent.prechat.save:Email" value="Email__c" />
<input type="hidden" name="liveagent.prechat.save:Subject" value="Subject__c" />
</form>
</apex:outputPanel>
</body>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment