+ Start a Discussion
Mike @ G2Mike @ G2 

How to create an Apex trigger to pull the "chat transcript" object into the a case?

Hi All,


We are using an integration of Live Person and Salesforce that allows my agents to create cases from the Live person console, which then populates the chat transcripts into the case.


I now need to be able to pull one of the variables from the chat transcript object and include it in some of our escalation email templates. However as my chat transcripts are stored on the "chat" object, i need a trigger created that pulls the chat transcript information directly into the case. 


Can anyone help me with that?






Before you do anything I would check with live person and see if they have configured their application to be dynamic enough to handle your use case without any coding. See if they have a mapping setup page or something like that.


I see this with a lot of appexchange apps, they don't build their product to be extensible at all. As an app developer I am not asking for the keys to the kingdom I just want good APIs so I can support my users without bending backwards to do so.

Plus if you write these types of customizations you won't have to write features down the road.


Sorry for the soap box shouting.


On to your question Mike:

So I am not familiar with Live Person's data model but I think we can assume that their custom chat object is a child of a case. Being that you might have many chat sessions to support a case it would be weird to open a new case for every chat.

In that case your trigger might look something like this:


trigger CaseTrigger on Case (before insert){

   Set<Id> caseIds = new Set<Id>();
   Map<Id, liveperson__Chat__c> chatMap = new Map<Id, liveperson__Chat__c>();

   for(Case c : trigger.new){

   for(liveperson__Chat__c chat : [select Id, [whatever fields you want] from liveperson__Chat__c where liveperson__Case__c in : caseIds){
      chatMap.put(chat.liveperson__Case__c, chat);

   for(Case c : trigger.new){
         c.[Your case custom field] = chatMap.get(c.Id).[liveperson field];


 Now this is not production ready code this will just get you started. You need to test it and write test methods. I did not compile this so it might not do that.


And you might notice that you are at the edge of an infinite loop. So with this trigger it will fire when live person creates a case from a chat being saved, so live person already has a trigger on after insert of chat. SO if you write a case trigger that inserts a chat well then you will never get out of that loop.


Before I even try and implement this code I would talk to live person and give them your use case. They might have a way to support it with out custom code.