Our second chapter will revolve around Data Modeling and Management which consists of 7% of total score in Salesforce Platform Dev II Exam. Without further ado, let’s get into it!
NOTE: This post is written in July 2019 and content might be changed/updated overtime. The content is inspired by focusonforce.com.
Introduction
- The powerful
Salesforceapplication is able to adapt to variations of language, locale and currency:- Language - controls label and translations
- Locale - controls data formatting such as dates, times, and names
- Currency - controls display of currency, such as currency symbols and use of period and comma for decimal places.
- Useful functions in
System.UserInfoclass:getDefaultCurrency()getFirstName()getLanguage()getLocale()getName()- get full namegetOrganizationId()getOrganizationName()getSessionId()getTimeZone()getUserEmail()getUserId()- return the user IdgetUserName()isMultiCurrencyOrganization()
Multi-Language
Languagecontrols labels and translations.- Salesforce provides 3 levels of language support:
- Fully Supported Languages - all standard pages and features automatically translate based on the user’s selected language.
- End-user Languages - all standard objects and pages, except administrative pages, Setup and Help
- Platform-only Languages - developer-sepcified translations for languages not supported by Salesforce, mainly used in custom objects, labels and fields.
Translation Workbenchcan be used add or activate languages in Salesforce.

- You can select the
Languageand then selectSetup Componentthat you would like to translate.


- You can also create
Custom Labeland set the translation to display user friendly text.



- In formula, label can be accessed via
$Label.Testing_Text; meanwhile in Apex, simply writeLabel.Testing_Textwill do. - To make error message more “translation friendly”, you can type like this:
ApexPages.addMessage(
new ApexPages.Message(ApexPages.Severity.ERROR, Label.Testing_Test)
);Multi-Locale
Localecontrols formatting such as number, date, time, name and etc.- Locale-aware output components such as
<apex:outputField>should be used inVisualforceso that data format can be displayed correctly.
Multi-Currency
Currencycontrols display of currency, such as currency symbols, period/comma for decimal places.- All objects that have currency field will have CurrencyIsoCode field to specify the currency that is used (available in multi-currency org)
- In
SOQL,convertCurrency()function can be used to convert currency andformat()function can be used in SOQL to format number, date, time, fields based on user’s locale, ex:SELECT format(convertCurrency(Amount)) from Opportunity
Custom Settings
Custom settingsallows to store custom sets of data in application cache for faster and frequent access.- NOTE: querying
Custom Settingsdoes not count towardsSOQLlimit! - Two types of
Custom Settings:- List Custom Settings (unusable) - provide reusable static data that is same for all users across org (preferred
Custom Metadata Type) - Hierarchy Custom Settings - use built-in hierarchical logic to personalize settings based on specific user profiles or users

- List Custom Settings (unusable) - provide reusable static data that is same for all users across org (preferred
- NOTE:
Custom Settingscan be included in a package, but the data will not. If needed, it must be done viaApex. Custom fieldscan be added inCustom Settings. Supported data types are checkbox, currency, date, date/time, email, number, percent, phone, text, textarea, and URL.

- Default value can be defined in organization level as well as by specific user or user’s profile.



Custom Settingscan be used in formulas, validation rules, flows, Apex and SOAP API.Custom Settingshave the same naming format as custom objects, as the API name ends in __c. For example,Custom Settingsnamed Test Hierarchy will have API name ofTest_Hierarchy__c.Custom Settingscan be assessed via$Setupin formula or validations. Example:$Setup.Test_Hierarchy__c.Test_Text__c

- To use
Custom SettingsinApex, simply typeTest_Hierarchy__c.getInstance().Test_Text__cwill do. - To prevent values of
Custom Settingsto be modified in managed packages, the Privacy can be set to Protected. - Use
Custom Settingswhen:- data doesn’t change frequently but needs to be referenced frequently
- values of
Custom Settingsneed to change based on logged-in user or user’s profile. - page layout needs to change based on different profiles or users
- specific value of setting will differ for every organization
- common value is needed to use in formula or validations.
- common currency value is needed.
Custom Metadata Types
Custom Metadata Typesare newer and preferred overCustom Settingswhich allows for both structure and actual record data to be included in a package.


- Unlike
Custom Settings,Custom Metadata Typescan create picklist, text area(long) and metadata relationships (another metadata type, entity definition, or field definition).

- NOTE: while creating custom picklist field, the “Use first value as default value” and “Restrict picklist to the values defined in the value set” are checked by default.

- Records can be created by clicking Manage button.


- Records can be referenced in formula fields.

- Sample
Custom Metadata Typesformula usage:$CustomMetadata.Custom_Test__mdt.Hello_World.Test_Picklist__c,$CustomMetadata.Custom_Test__mdt.Hello_World.MasterLabel- Access
Custom Metdata TypesviaSOQL:SELECT Label, Test_Picklist__c FROM Custom_Test__mdt WHERE Test_Picklist__c = 'A'
- In managed packages,
Custom Metadata Typescan be protected at the Type, Record, and Field Level.- Public types can be used by all Apex code in an org.
- Protected types can be used by package
- Protected records can only be accessed by code in the same namespace as the record or its type
- Combination of Public type and Protected record can be used to allow package subscribers to add their own values to a type, but not to modify the type structure.
- Fields have 3 levels of editing privileges:
- Upgradable - Only the package developer (via package upgrade)
- Subscriber editable - Any user with Custom Application permission (package upgrades won’t override the value)
- Locked after release - No one

- Use
Custom Metadata Typeswhen:- data doesn’t change frequently but needs to be referenced frequently
- values of the settings are same for all users and profiles (can’t be specifically defined)
- values are tied to a package and are the same for any subscribing organization
- A relationship, common picklist or long text area is needed for a package
- Parameters for formulas used in formula fields can be stored
- Unlike custom settings,
Custom Metadata Typesare always accessible by all users.
| Feature | Custom Settings | Custom Metadata Types |
|---|---|---|
| Deploy configuration | ✅ | ✅ |
| Deploy configurable data | ❌ | ✅ |
| Test classes visibility | ❌ | ✅ |
| Create/update in bulk custom metadata loader | ❌ | ✅ |
| Relationship type | ❌ | ✅ |
| Picklist field type | ❌ | ✅ |
| Long text area field type | ❌ | ✅ |
| Currency field type | ✅ | ❌ |
| Exempt SOQL limits | ✅ | ✅ |
| Support hierarchy configuration | ✅ | ❌ |
| CRUD via Apex | ✅ | ✅ (no delete) |
| Accessible by formula fields (hierarchy) | ✅ | ✅ |