We're using the service cloud console and live agent chat. We have chosen not to automatically create a new case for every inbound chat request (So Deployment API and pre-chat API are probably not in play here).

We would like to automatically populate the account and case lookups on the chat transcript based on what the agent is doing/looking at.

We already have APEX code that executes when agents view an account and when they create a new case, so we can add additional code that performs the association. The code can be straight APEX, Javascript using the console integration toolkit, or a combination of both.

Ideally, the method would allow the association to be picked up by the normal "Attach" feature of the live chat window (the paperclip).

Thanks. All ideas are appreciated.

  • did you ever come up with a solution for this? I'm thinking I may have to query for the LiveChatVisitor by the chat key then associate our case to the most recent LiveChatTranscript related to the visitor...
    – Doug Ayers
    Aug 25, 2016 at 19:27
  • No @DougAyers we never successfully solved this issue. I'd be really interested to know if your suggested approach works though. If so, I'd appreciate a solution here for everyone to see.
    – Mike Ginou
    Aug 30, 2016 at 20:22
  • I have an idea how to get it to work, will put POC together and post as answer hopefully sometime today!
    – Doug Ayers
    Sep 1, 2016 at 13:47

1 Answer 1


When manually creating the Case in a console component instead of letting Salesforce create the Case automatically via pre-chat or deployment API then you will need to use an apex trigger on the LiveChatTranscript object and add a custom text field Chat Key to your Case object to correlate the records in the trigger.

Custom Console Component

Here is sample javascript that listens for a new chat session to start and creates a new Case record, populating the Case record with the Chat Key.


    <apex:includeScript value="/support/console/37.0/integration.js"/>

        <apex:remoteObjectModel name="Case" fields="Id,Chat_Key__c"/>

    <script type="text/javascript">

        // on chat start, create case manually and populate the chat_key__c field.
        // once agent closes the chat/case tabs then salesforce creates transcript.
        // because we created case manually and not through pre-chat or deployment api then
        // salesforce does not automatically link the transcript to this case, that is
        // where the LiveChatTranscriptTrigger comes into play and links them for us.

        sforce.console.chat.onChatStarted( function( chatResult ) {

            if ( chatResult.success ) {

                // create case and set the chat_key__c field
                // you can use the sforce.connection API, REST API, Remote Actions, etc.
                // just make sure when you create the case you get that chat key field populated

                var cs = new SObjectModel.Case({
                    Subject : 'Live Chat Test',
                    Origin : 'Web',
                    Chat_Key__c : chatResult.chatKey

                cs.create( function( err, records, evt ) {

                    console.log( err );
                    console.log( records );
                    console.log( evt );

                    if ( err ) {

                        alert( err.message );

                    } else {

                        sforce.console.getFocusedPrimaryTabId( function( primaryTabResult ) {

                            console.log( 'getFocusedPrimaryTabId' );
                            console.log( primaryTabResult );

                            if ( primaryTabResult.success ) {

                                console.log( 'primaryTabId: ' + primaryTabResult.id );
                                console.log( 'caseId: ' + records[0] );

                                    '/' + records[0] + '/e',
                                    'Edit Case',
                                    function( openSubtabResult ) {
                                        console.log( 'openSubtabResult' );
                                        console.log( openSubtabResult );
                                ); // end open sub tab

                            } // end if

                        }); // end get focused primary tab id

                    } // end if

                }); // end case create callback

            } // end if

        }); // end on chat started callback



LiveChatTranscript Trigger

The LiveChatTranscript record will have the ChatKey populated on it but the CaseId will be blank (in this scenario). So we look up the related Case indirectly by the chat key custom field we setup earlier.

trigger LiveChatTranscriptTrigger on LiveChatTranscript ( after insert ) {

    Set<String> chatKeys = new Set<String>();

    for ( LiveChatTranscript transcript : Trigger.new ) {
        if ( String.isBlank( transcript.caseId ) && String.isNotBlank( transcript.chatKey ) ) {
            chatKeys.add( transcript.chatKey );

    System.debug( 'chatKeys=' + chatKeys );

    if ( chatKeys.size() > 0 ) {

        // chat_key__c should be a unique, external id field
        // populated in the console once the chat has started and case created
        List<Case> cases = new List<Case>([
                id, chat_key__c
                chat_key__c IN :chatKeys
            ORDER BY
                createdDate ASC

        // chatKey => case
        Map<String, Case> chatKeyCasesMap = new Map<String, Case>();

        for ( Case cs : cases ) {
            chatKeyCasesMap.put( cs.chat_key__c, cs );

        System.debug( 'chatKeyCasesMap=' + chatKeyCasesMap );

        List<LiveChatTranscript> transcriptsToUpdate = new List<LiveChatTranscript>();

        for ( LiveChatTranscript transcript : Trigger.new ) {

            if ( String.isBlank( transcript.caseId ) && String.isNotBlank( transcript.chatKey ) ) {

                Case cs = chatKeyCasesMap.get( transcript.chatKey );

                if ( cs != null ) {
                    transcriptsToUpdate.add( new LiveChatTranscript(
                        id = transcript.id,
                        caseId = cs.id



        if ( transcriptsToUpdate.size() > 0 ) {
            System.debug( 'transcriptsToUpdate: ' + transcriptsToUpdate );
            update transcriptsToUpdate;



Unit Test

Simple unit test for code coverage of above trigger.

private class LiveChatTranscriptTriggerTest {

    static void link_transcript_to_case() {

        Case cs = new Case(
            subject = 'Test Case',
            origin = 'Web',
            chat_key__c = '123'

        insert cs;

        LiveChatVisitor visitor = new LiveChatVisitor();

        insert visitor;


        LiveChatTranscript tx = new LiveChatTranscript(
            chatKey = '123',
            liveChatVisitorId = visitor.id

        insert tx;


        tx = [ SELECT id, caseId, chatKey FROM LiveChatTranscript WHERE id = :tx.id ];

        System.assertEquals( cs.chat_key__c, tx.chatKey );
        System.assertEquals( cs.id, tx.caseId );


  • Thanks Doug. Nice use of the Winter '17 change to create transcripts when the chat starts. I can't accept this until I dig a bit deeper into it though.
    – Mike Ginou
    Sep 23, 2016 at 20:59
  • Thanks Doug. The example here doesn't quite match what we were trying to do, but I think all it would take is re-arranging the concepts. Nice.
    – Mike Ginou
    Sep 23, 2016 at 21:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .