Skip to content

Instantly share code, notes, and snippets.

@danieljpeter
Created December 12, 2012 03:23
Show Gist options
  • Save danieljpeter/4264616 to your computer and use it in GitHub Desktop.
Save danieljpeter/4264616 to your computer and use it in GitHub Desktop.
This is a trigger and Apex Class to set Lead Owners which are related Live Agent transcripts to the same owner as the transcript. This is because the leads are created as owned by the anon user for the force.com site the prechat form is hosted on. Also a test class for the trigger. See this https://gist.github.com/4264650 for the prechat form an…
@isTest
private class testTriggerLiveChatTranscript {
static final Id SALES_SITE_GUEST_USER = '005000000070000';
static testMethod void doTest() {
//create a test Lead
Lead testLead = new Lead(OwnerId = SALES_SITE_GUEST_USER,
FirstName = 'John',
LastName = 'Smith',
Email = 'test@test.com',
LeadSource = 'Corporate Website, Live Chat',
Company = 'ACME');
insert testLead;
update testLead;
//create a Live Chat Visitor
LiveChatVisitor visitor = new LiveChatVisitor();
insert visitor;
update visitor;
//create a Live Chat Transcript
LiveChatTranscript trans = new LiveChatTranscript(
LiveChatVisitorId = visitor.Id,
LeadId = testLead.Id,
Email__c = 'test@test.com',
FirstName__c = 'John',
LastName__c = 'Smith',
Body = 'Some chat. Blah Blah'
);
insert trans;
}
}
public class LeadUpdateOwnerChat {
//the ID of the anon user for the force.com site your prechat form is hosted on
static final Id SALES_SITE_GUEST_USER = '005000000000000';
@future
public static void updateLeadOwner(Map<Id, Id> leadOwnerMap) {
//map coming has the key as the lead id and the value as the new owner id
//build a list of the Leads we need to change the owner on
if (!leadOwnerMap.isEmpty()) {
List<Lead> leadList = new List<Lead>();
leadList = [SELECT Id, OwnerId FROM Lead WHERE Id IN: leadOwnerMap.keySet() AND OwnerId=: SALES_SITE_GUEST_USER];
if (!leadList.isEmpty()) {
for (Lead l : leadList) {
l.OwnerId = leadOwnerMap.get(l.Id);
}
update leadList;
}
}
}
static testMethod void doTest() {
//create a test Lead
Lead testLead = new Lead(OwnerId = SALES_SITE_GUEST_USER,
FirstName = 'John',
LastName = 'Smith',
Email = 'test@test.com',
LeadSource = 'Corporate Website, Live Chat',
Company = 'ACME');
insert testLead;
update testLead;
//create a test sales rep
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u = new User(Alias = 'test4a6', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='test4a6@test4a6.com');
insert u;
update u;
Map<Id, Id> leadOwnerMap = new Map<Id, Id>();
leadOwnerMap.put(testLead.Id, u.Id);
Test.startTest();
updateLeadOwner(leadOwnerMap);
Test.stopTest();
}
}
trigger LiveChatTranscriptUpdateLead on LiveChatTranscript (before insert, before update) {
//build a map where the key is the LeadId and the value is the new ownerID
Map<Id, Id> leadOwnerMap = new Map<Id, Id>();
for (LiveChatTranscript ts : Trigger.new) {
if (ts.LeadId != null) {
leadOwnerMap.put(ts.LeadId, ts.OwnerId);
}
}
if (!LeadOwnerMap.isEmpty()) {
LeadUpdateOwnerChat.updateLeadOwner(leadOwnerMap);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment