Tuesday, November 15, 2016

All the datetime fields in the Userlocale

Use Case

Every time Clients wants all the datetime fields in the Userlocale. Let say, client is having salesforce enable for different countries. When USER of USA will be logged in into salesforce, the date fields should be in USA Format in VF and if any user will be logged in from india, then date format should be in INDIA format. But in salesforce, we do not have any method of showing these fields into user locale format.


Solution

The solution is to use Dynamic Apex components to get the User Locale with all DateTime fields. Wherever you want to show fields into userlocale, you need to add this component into VF.


Output:-

Code Snippet


Apex Class

/*
                                Description:controller for the locale_formatted_datetime Visualforce component
*/
public class controller_locale_formatted_datetime {
               
                public DateTime date_time { get; set; } //property that reads the datetime value from the component attribute tag
               
                //returns the properly formatted datetime value
                public String getTimeZoneValue() {
                                Map<String, String> mappedValues = new Map<String, String>(); //map for holding locale to datetime format
                                mappedValues = MapValues(); //populate the map with all the locale specific datetime formats
                                String user_locale = UserInfo.getLocale(); //grab the locale of the user
                                String datetime_format = 'M/d/yyyy h:mm a'; //variable for the datetime format defaulted to the US format
                                if (mappedValues.containsKey(user_locale)) { //if the map contains the correct datetime format
                                                datetime_format = mappedValues.get(user_locale); //grab the datetime format for the locale
                                }
                                String locale_formatted_date_time_value = date_time.format(datetime_format); //create a string with the proper format
                                return locale_formatted_date_time_value; //return the string
                }
               
                //populate a map with locale values and corresponding datetime formats
                private Map<String, String> MapValues() {
                                Map<String, String> locale_map = new Map<String, String>(); //holds the locale to timedate formats
                                locale_map.put('ar', 'dd/MM/yyyy hh:mm a');
                                locale_map.put('ar_AE', 'dd/MM/yyyy hh:mm a');
                                locale_map.put('ar_BH', 'dd/MM/yyyy hh:mm a');
                                locale_map.put('ar_JO', 'dd/MM/yyyy hh:mm a');
                                locale_map.put('ar_KW', 'dd/MM/yyyy hh:mm a');
                                locale_map.put('ar_LB', 'dd/MM/yyyy hh:mm a');
                                locale_map.put('ar_SA', 'dd/MM/yyyy hh:mm a');
                                locale_map.put('bg_BG', 'yyyy-M-d H:mm');
                                locale_map.put('ca', 'dd/MM/yyyy HH:mm');
                                locale_map.put('ca_ES', 'dd/MM/yyyy HH:mm');
                                locale_map.put('ca_ES_EURO', 'dd/MM/yyyy HH:mm');
                                locale_map.put('cs', 'd.M.yyyy H:mm');
                                locale_map.put('cs_CZ', 'd.M.yyyy H:mm');
                                locale_map.put('da', 'dd-MM-yyyy HH:mm');
                                locale_map.put('da_DK', 'dd-MM-yyyy HH:mm');
                                locale_map.put('de', 'dd.MM.yyyy HH:mm');
                                locale_map.put('de_AT', 'dd.MM.yyyy HH:mm');
                                locale_map.put('de_AT_EURO', 'dd.MM.yyyy HH:mm');
                                locale_map.put('de_CH', 'dd.MM.yyyy HH:mm');
                                locale_map.put('de_DE', 'dd.MM.yyyy HH:mm');
                                locale_map.put('de_DE_EURO', 'dd.MM.yyyy HH:mm');
                                locale_map.put('de_LU', 'dd.MM.yyyy HH:mm');
                                locale_map.put('de_LU_EURO', 'dd.MM.yyyy HH:mm');
                                locale_map.put('el_GR', 'd/M/yyyy h:mm a');
                                locale_map.put('en_AU', 'd/MM/yyyy HH:mm');
                                locale_map.put('en_B', 'M/d/yyyy h:mm a');
                                locale_map.put('en_BM', 'M/d/yyyy h:mm a');
                                locale_map.put('en_CA', 'dd/MM/yyyy h:mm a');
                                locale_map.put('en_GB', 'dd/MM/yyyy HH:mm');
                                locale_map.put('en_GH', 'M/d/yyyy h:mm a');
                                locale_map.put('en_ID', 'M/d/yyyy h:mm a');
                                locale_map.put('en_IE', 'dd/MM/yyyy HH:mm');
                                locale_map.put('en_IE_EURO', 'dd/MM/yyyy HH:mm');
                                locale_map.put('en_NZ', 'd/MM/yyyy HH:mm');
                                locale_map.put('en_SG', 'M/d/yyyy h:mm a');
                                locale_map.put('en_US', 'M/d/yyyy h:mm a');
                                locale_map.put('en_ZA', 'yyyy/MM/dd hh:mm a');
                                locale_map.put('es', 'd/MM/yyyy H:mm');
                                locale_map.put('es_AR', 'dd/MM/yyyy HH:mm');
                                locale_map.put('es_BO', 'dd-MM-yyyy hh:mm a');
                                locale_map.put('es_CL', 'dd-MM-yyyy hh:mm a');
                                locale_map.put('es_CO', 'd/MM/yyyy hh:mm a');
                                locale_map.put('es_CR', 'dd/MM/yyyy hh:mm a');
                                locale_map.put('es_EC', 'dd/MM/yyyy hh:mm a');
                                locale_map.put('es_ES', 'd/MM/yyyy H:mm');
                                locale_map.put('es_ES_EURO', 'd/MM/yyyy H:mm');
                                locale_map.put('es_GT', 'd/MM/yyyy hh:mm a');
                                locale_map.put('es_HN', 'MM-dd-yyyy hh:mm a');
                                locale_map.put('es_MX', 'd/MM/yyyy hh:mm a');
                                locale_map.put('es_PE', 'dd/MM/yyyy hh:mm a');
                                locale_map.put('es_PR', 'MM-dd-yyyy hh:mm a');
                                locale_map.put('es_PY', 'dd/MM/yyyy hh:mm a');
                                locale_map.put('es_SV', 'MM-dd-yyyy hh:mm a');
                                locale_map.put('es_UY', 'dd/MM/yyyy hh:mm a');
                                locale_map.put('es_VE', 'dd/MM/yyyy hh:mm a');
                                locale_map.put('et_EE', 'd.MM.yyyy H:mm');
                                locale_map.put('fi', 'd.M.yyyy H:mm');
                                locale_map.put('fi_FI', 'd.M.yyyy H:mm');
                                locale_map.put('fi_FI_EURO', 'd.M.yyyy H:mm');
                                locale_map.put('fr', 'dd/MM/yyyy HH:mm');
                                locale_map.put('fr_BE', 'd/MM/yyyy H:mm');
                                locale_map.put('fr_CA', 'yyyy-MM-dd HH:mm');
                                locale_map.put('fr_CH', 'dd.MM.yyyy HH:mm');
                                locale_map.put('fr_FR', 'dd/MM/yyyy HH:mm');
                                locale_map.put('fr_FR_EURO', 'dd/MM/yyyy HH:mm');
                                locale_map.put('fr_LU', 'dd/MM/yyyy HH:mm');
                                locale_map.put('fr_MC', 'dd/MM/yyyy HH:mm');
                                locale_map.put('hr_HR', 'yyyy.MM.dd HH:mm');
                                locale_map.put('hu', 'yyyy.MM.dd. H:mm');
                                locale_map.put('hy_AM', 'M/d/yyyy h:mm a');
                                locale_map.put('is_IS', 'd.M.yyyy HH:mm');
                                locale_map.put('it', 'dd/MM/yyyy H.mm');
                                locale_map.put('it_CH', 'dd.MM.yyyy HH:mm');
                                locale_map.put('it_IT', 'dd/MM/yyyy H.mm');
                                locale_map.put('iw', 'HH:mm dd/MM/yyyy');
                                locale_map.put('iw_IL', 'HH:mm dd/MM/yyyy');
                                locale_map.put('ja', 'yyyy/MM/dd H:mm');
                                locale_map.put('ja_JP', 'yyyy/MM/dd H:mm');
                                locale_map.put('kk_KZ', 'M/d/yyyy h:mm a');
                                locale_map.put('km_KH', 'M/d/yyyy h:mm a');
                                locale_map.put('ko', 'yyyy. M. d a h:mm');
                                locale_map.put('ko_KR', 'yyyy. M. d a h:mm');
                                locale_map.put('lt_LT', 'yyyy.M.d HH.mm');
                                locale_map.put('lv_LV', 'yyyy.d.M HH:mm');
                                locale_map.put('ms_MY', 'dd/MM/yyyy h:mm a');
                                locale_map.put('nl', 'd-M-yyyy H:mm');
                                locale_map.put('nl_BE', 'd/MM/yyyy H:mm');
                                locale_map.put('nl_NL', 'd-M-yyyy H:mm');
                                locale_map.put('nl_SR', 'd-M-yyyy H:mm');
                                locale_map.put('no', 'dd.MM.yyyy HH:mm');
                                locale_map.put('no_NO', 'dd.MM.yyyy HH:mm');
                                locale_map.put('pl', 'yyyy-MM-dd HH:mm');
                                locale_map.put('pt', 'dd-MM-yyyy H:mm');
                                locale_map.put('pt_AO', 'dd-MM-yyyy H:mm');
                                locale_map.put('pt_BR', 'dd/MM/yyyy HH:mm');
                                locale_map.put('pt_PT', 'dd-MM-yyyy H:mm');
                                locale_map.put('ro_RO', 'dd.MM.yyyy HH:mm');
                                locale_map.put('ru', 'dd.MM.yyyy H:mm');
                                locale_map.put('sk_SK', 'd.M.yyyy H:mm');
                                locale_map.put('sl_SI', 'd.M.y H:mm');
                                locale_map.put('sv', 'yyyy-MM-dd HH:mm');
                                locale_map.put('sv_SE', 'yyyy-MM-dd HH:mm');
                                locale_map.put('th', 'M/d/yyyy h:mm a');
                                locale_map.put('th_TH', 'd/M/yyyy, H:mm ?.');
                                locale_map.put('tr', 'dd.MM.yyyy HH:mm');
                                locale_map.put('ur_PK', 'M/d/yyyy h:mm a');
                                locale_map.put('vi_VN', 'HH:mm dd/MM/yyyy');
                                locale_map.put('zh', 'yyyy-M-d ah:mm');
                                locale_map.put('zh_CN', 'yyyy-M-d ah:mm');
                                locale_map.put('zh_HK', 'yyyy-M-d ah:mm');
                                locale_map.put('zh_TW', 'yyyy/M/d a h:mm');
                                return locale_map; //return the map
                }

}

VF Component

<apex:component access="global" controller="controller_locale_formatted_datetime">
<!--
                Description:When adding to your Salesforce org set the name of component to locale_formatted_datetime
-->
<apex:attribute assignTo="{!date_time}" description="Rendered DateTime with the user's locale" name="date_time_value" type="DateTime"></apex:attribute>
{!TimeZoneValue}
</apex:component>

VF Page

<apex:page standardController="Account" tabStyle="Account">

<apex:sectionHeader title="Account Page" subtitle="{!Account.Name}"></apex:sectionHeader>
    <apex:pageMessages></apex:pageMessages>
    <apex:form>
        <apex:pageBlock title="Account Information" mode="detail">
            <apex:pageBlockSection columns="2" title="General Information">
                <apex:outputField value="{!Account.Name}"></apex:outputField>
                <apex:outputField value="{!Account.OwnerId}"></apex:outputField>
                </apex:pageBlockSection>
            <!--**************************************OLD Information********************************-->
            <apex:pageBlockSection columns="2" title="Unformatted System Information">
                <apex:outputField value="{!Account.CreatedById}">
                    <apex:outputText value=", {0,date,M/d/yyyy h:mm a}">
                        <apex:param value="{!Account.CreatedDate}"></apex:param>
                    </apex:outputText>
                </apex:outputField>
                <apex:outputField value="{!Account.LastModifiedById}">, <apex:outputText value="{!Account.LastModifiedDate}"></apex:outputText></apex:outputField>
            </apex:pageBlockSection>
            <!--****************************************Formatted Information********************************-->
            <apex:pageBlockSection columns="2" title="Formatted System Information">
                <apex:outputField value="{!Account.CreatedById}">, <c:locale_formatted_datetime date_time_value="{!Account.CreatedDate}"></c:locale_formatted_datetime></apex:outputField>
                <apex:outputField value="{!Account.LastModifiedById}">, <c:locale_formatted_datetime date_time_value="{!Account.LastModifiedDate}"></c:locale_formatted_datetime></apex:outputField>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page> 

Test Class

/*
              
                Description: tests the controller_locale_formatted_datetime class
*/
@isTest
private class test_locale_formatted_datetime {
               
                static testMethod void test_logic() {
                                //test for English (United States) format
                                List<User> userUpdate = new List<User>(); //list for holding user updates
                                userUpdate.add(new User(Id = UserInfo.getUserId(), LocaleSidKey = 'en_US')); //set the locale for the running user to English (United States)
                                update userUpdate; //make the update
                                controller_locale_formatted_datetime controller = new controller_locale_formatted_datetime();
                                controller.date_time = DateTime.valueOf('2007-01-01 2:35:21'); //set the datetime variable to 1 January 2007
                                String test_value = controller.getTimeZoneValue(); //run the logic and format the datetime value
                                System.assertEquals('1/1/2007 2:35 AM', test_value); //validate the results
                               
                                //now test for Arabic (Saudi Arabia) format
                                userUpdate.clear(); //remove all user sobjects from the userUpdate List
                                userUpdate.add(new User(Id = UserInfo.getUserId(), LocaleSidKey = 'ar_SA')); //set the locale for the running user to Arabic (Saudi Arabia)
                                update userUpdate; //make the update
                                controller.date_time = DateTime.valueOf('2005-03-07 5:02:21'); //set the datetime variable to 7 March 2005
                                test_value = controller.getTimeZoneValue(); //run the logic and format the datetime value
                                System.assertEquals('07/03/2005 05:02 AM', test_value); //validate the results

                                //now test for Chinese (Taiwan) format
                                userUpdate.clear(); //remove all user sobjects from the userUpdate List
                                userUpdate.add(new User(Id = UserInfo.getUserId(), LocaleSidKey = 'zh_TW')); //set the locale for the running user to Chinese (Taiwan)
                                update userUpdate; //make the update
                                controller.date_time = DateTime.newInstance(2011, 1, 3, 12, 41, 15); //set the datetime variable to 3 January 2011
                                test_value = controller.getTimeZoneValue(); //run the logic and format the datetime value
                                System.assertEquals('2011/1/3 PM 12:41', test_value); //validate the results
                }
}

No comments:

Post a Comment