Created
April 8, 2013 22:07
-
-
Save danieljpeter/5340968 to your computer and use it in GitHub Desktop.
Revisions
-
danieljpeter created this gist
Apr 8, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ trigger LeadConvertReparentChatTranscript on Lead (before update) { Map<Id,Id> convertedLeadsWithContactIds = new Map<Id,Id>(); for (Lead theLead:Trigger.new) { Lead beforeUpdate = System.Trigger.oldMap.get(theLead.Id); if ((theLead.ConvertedContactId != null) && (beforeUpdate.ConvertedContactId == null)) { //if the converted contactId just went from not populated, to populated //meaning we are converting the lead into a contact convertedLeadsWithContactIds.put(theLead.Id,theLead.ConvertedContactId); } } if (!convertedLeadsWithContactIds.isEmpty()) { LeadReparentChatTranscript.doUpdate(convertedLeadsWithContactIds); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ public class LeadReparentChatTranscript { public static void doUpdate(Map<Id,Id> convertedLeadsWithContactIds) { //convertedLeadsWithContactIds, key is the LeadId, value is the ConvertedContactId; //go through and get all the corresponding Transcript records List<LiveChatTranscript> tList = new List<LiveChatTranscript>(); tList = [SELECT Id, LeadId, ContactId FROM LiveChatTranscript WHERE LeadId IN: convertedLeadsWithContactIds.keySet() LIMIT 10000]; if (!tList.isEmpty()) { for(LiveChatTranscript l : tList) { l.ContactId = convertedLeadsWithContactIds.get(l.LeadId); } update tList; } } static testMethod void doTest() { Map<Id,Id> convertedLeadsWithContactIds = new Map<Id,Id>(); //create a contact Contact testContact = new Contact(LastName='Smith'); insert testContact; update testContact; //create a lead Lead testLead = new Lead(LastName='Smith', Company='ACME'); insert testLead; update testLead; //create a live chat visitor LiveChatVisitor testVisit = new LiveChatVisitor(); insert testVisit; update testVisit; //create a transcript LiveChatTranscript testTrans = new LiveChatTranscript(LeadId=testLead.Id, LiveChatVisitorId=testVisit.Id); insert testTrans; update testTrans; convertedLeadsWithContactIds.put(testLead.Id, testContact.Id); LeadReparentChatTranscript.doUpdate(convertedLeadsWithContactIds); } }