2

I'm creating a Pre Chat and Post Chat page for Live Agent. The Pre Chat page inserts a Case. I need the Post Chat page to access that Case or get its Id. How do I do that?

Thanks in advance.

1

In post chat page the Case Id is available through a page parameter named as attachedRecords. It is a JSON string and for me it is coming as {"ContactId":"003i000002nB9wFAAS","Case":"500i000000hkMtSAAU"}. You have to parse this JSON to get the Case ID.

I have created JSON2Apex class using https://json2apex.herokuapp.com/ to parse the JSON.

Here is my VF page and controller class code as below.

VF Page

<apex:page controller="PostChatController" showHeader="false" >
    CaseID : {!caseId}
</apex:page>

Controller

public class PostChatController {
    public String caseId {get;set;}
    public String attachedRecords {get;set;}
    public PostChatController() {
        attachedRecords = ApexPages.currentPage().getParameters().get('attachedRecords');
        caseId = JSON2Apex.parse(attachedRecords).Case_Z;
    }    
}

JSON2Apex Class

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//
// The supplied json has fields with names that are reserved words in apex
// and so can only be parsed with explicitly generated code, this option
// was auto selected for you.

public class JSON2Apex {
    public static void consumeObject(JSONParser parser) {
        Integer depth = 0;
        do {
            JSONToken curr = parser.getCurrentToken();
            if (curr == JSONToken.START_OBJECT || 
                curr == JSONToken.START_ARRAY) {
                depth++;
            } else if (curr == JSONToken.END_OBJECT ||
                curr == JSONToken.END_ARRAY) {
                depth--;
            }
        } while (depth > 0 && parser.nextToken() != null);
    }

    public String ContactId {get;set;} 
    public String Case_Z {get;set;} // in json: Case

    public JSON2Apex(JSONParser parser) {
        while (parser.nextToken() != JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != JSONToken.VALUE_NULL) {
                    if (text == 'ContactId') {
                        ContactId = parser.getText();
                    } else if (text == 'Case') {
                        Case_Z = parser.getText();
                    } else {
                        System.debug(LoggingLevel.WARN, 'Root consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }


    public static JSON2Apex parse(String json) {
        return new JSON2Apex(System.JSON.createParser(json));
    }
}
0

If a Case was inserted, the Case Id will be provided in the AttachedRecords parameter. Below is code you can use in the Past Chat Page:

Attached Records : <apex:outputText value="{!$CurrentPage.parameters.attachedRecords}" />

I took that line from Salesforce's Post Chat Page sample code.

Keep in mind that you need to let everything load for the LiveAgent user. If you are doing testing and you start the chat and end it immediately, then you won't see the Case Id in the AttachedRecords parameter. You must start the chat, then actually wait for everything to load and display on the LiveAgent's page. Then, once everything is loaded, you can end the chat and you will see the Case Id. My problem was I was going too fast.

  • How this is different from my answer. I have already mentioned about attachedRecords. – Saroj Bera Jun 20 '16 at 14:13
  • Hi Mike, welcome to SFSE. If someone's answer helped you then you should really accept theirs rather than posting the relevant part and accepting your own answer. – Matt Lacey Jun 21 '16 at 0:20
  • Sorry, this was literally my first time using this site. I thought you could checkmark 2 answers as "correct" at the same time. I've now checkmarked Saroj's answer as correct. I did add my own answer to this question because I wanted to add more details. The reason the Case Id wasn't showing up in the Attached Records parameter was because I was closing the chat window too fast in my testing. I thought it was important to add that note in this discussion. That is why I added my own answer. – Mike Jun 21 '16 at 17:14
  • Mike you are welcome and thanks for the additional info which is very helpful. – Saroj Bera Jun 21 '16 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.