function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mugdha KshirsagarMugdha Kshirsagar 

How to create case automatically with the chat contents in live agent using apex triggers?

Hi,

I am new to apex and coding as well. I want to know how to create case record when user is done with the chat through live agent and clicks on end chat button.
Best Answer chosen by Mugdha Kshirsagar
pconpcon
This is pretty simple to do.  You just create a trigger on the LiveChatTranscript object.  Then pull the data from that object.  You should have the ContactId, AccountId, OwnerId at your disposal.  I recommend doing this in a before trigger so that you can then update the LiveChatTranscript object withe the new CaseId that you just created for it.

All Answers

pconpcon
This is pretty simple to do.  You just create a trigger on the LiveChatTranscript object.  Then pull the data from that object.  You should have the ContactId, AccountId, OwnerId at your disposal.  I recommend doing this in a before trigger so that you can then update the LiveChatTranscript object withe the new CaseId that you just created for it.
This was selected as the best answer
Mugdha KshirsagarMugdha Kshirsagar
Thanks a lot for your answer. I will try to write the trigger now. Is there any other settings I need to change In live agent? Do I heed to call api? 
I have implemented live agent internally in my developer edition.
pconpcon
No, you should be able to just write a trigger without any changes.
mounika nunnagopulamounika nunnagopula
Hi Mugdha,

Can you put your code, i would like to see how it works. I have a similar requirement.


 
Mugdha RalegaonkarMugdha Ralegaonkar
the code is to set description on the case after the case is created from the chat.

trigger SetDescOnCase on LiveChatTranscript (after insert) {     LiveChatTranscript[] scripts = Trigger.new;     if(scripts == null)         return;     if(scripts.size() <= 0)         return;     LiveChatTranscript script = scripts[0];     System.debug(script);     String body = script.Body;     String caseid = script.CaseId;     String email = '';     try     {         Case c = Database.query('SELECT Description  FROM Case WHERE Id = :caseid limit 1');         c.Description = script.Body.stripHtmlTags();         update c;      }      catch (Exception e){           System.debug(e.getMessage());      } }