Saturday, May 2, 2020

How to pass language to Custom Chat Page in SFDC Chat

How to pass language to Custom Chat Page


Problem Statement:
When we have started implementing SFDC chat functionality, there was a requirement to create multiple Chat Buttons based on Country and Language. So we decided to create a Pre-Chat Form page and multiple Custom Chat Page based on number of countries and Language since there is no options to pass Language to Custom Chat Page(standard browser behavior which ignores whatever comes after #). But we did not get approval to create that many number of VF pages from Architect team so decided to implement without creating multiple VF pages. We have searched many links, we did not get any information. Later we found a crazy idea to pass Language to Custom Chat Page and below is the idea.

Solution:
Idea is simple, in pre chat form page, we have to populate a hidden field from custom setting/ metadata based on button code. When we click on Chat now, SFDC internally, form an URL with all the inputs which we have entered in the pre-chat form page. Custom Chat page controller capture the URL which has all pre chat page inputs and assigns language. Code snippet below

Create Pre-Chat Form Page:

Pre chat Controller:

public class getLanguage{
    public String languageCode;
    public String getLangCode(){
String fullURL=ApexPages.currentPage().getUrl();
String[] subURLStrings=fullURL.split('%26');
for(string str:subURLStrings){
  if('button_id'.equalsIgnorecase(str.SubStringBefore('%3D'))){
   buttonId=str.SubStringAfter('%3D');
  }
}
if(buttonId != null){
Button_Language__c buttonLang = Button_Language__C.getInstance('buttonId');
languageCode = buttonLang.LangCode__c;
}
return languageCode;
    }
}


Pre-Chat Page:

along with other fields add below hidden field.
<apex:page id="pg" showHeader="false" controller="getLanguage" language="{!languageCode}"> 
.
.
.

<input type="hidden" name="liveagent.prechat:LanguageCode" value="{!languageCode}" />

<input type="hidden" name="liveagent.prechat.save:LanguageCode" value="Language__c" />
.
.
.
</apex:page>


Custom Chat Page:

Custom Chat page Controller:

public class getCustChatLanguage{
    public String langCode { get; set; }
    public String lang;
     
    //Method to get the language in chat page
    public getCustChatLanguage (){
    
        lang = JSON.serialize(ApexPages.currentPage().geturl());
        if(String.IsNotBlank(lang)){ 
        try{
            lang = lang.substringAfter('liveagent.prechat%3ALanguageCode='); 
            lang = lang.substring(0,5);
            langCode = lang;
            }catch(Exception e){
                System.debug(e.getmessage());
            }
        }
    }

}

Custom Chat Page:

<apex:page showHeader="false" controller="getCustChatLanguage" language="{!langCode}"> 

.
.
.
.
.
</apex:page>






No comments:

Post a Comment

Improve performance and speed in Lightning Experience

  Improve performance and speed in Lightning Experience:   Salesforce Performance Test: 1.     To access this tool, log-on to Salesfo...