Record Types, Page Layouts & Schema

0/4 in this phase0/41 across the roadmap

📖 Concept

Record Types allow you to offer different business processes, picklist values, and page layouts for the same object. They're a critical tool for building enterprise-grade solutions that serve multiple business units.

Record Types — when and why:

  • Different divisions sell different products using the same Opportunity object
  • Support teams handle different case categories with different fields
  • Multiple countries have different compliance fields on the Account object

How Record Types work:

  1. Each record type maps to a Page Layout (which fields the user sees)
  2. Each record type can have different picklist values (e.g., different Status options)
  3. Record types are assigned to Profiles — users only see record types assigned to their profile
  4. Record types create different record creation flows (different fields presented)
  5. Records store their record type in the RecordTypeId field

Page Layouts: Define the arrangement of fields, related lists, buttons, and components on a record page. Key capabilities:

  • Control field visibility and read-only status
  • Arrange fields in sections (1 or 2 columns)
  • Add related lists (child records)
  • Add custom buttons and links
  • Include Visualforce pages or Lightning components
  • Important: Page layouts control field visibility in the UI, NOT field-level security. FLS is separate.

Lightning Record Pages: Modern alternative to Page Layouts using Lightning App Builder:

  • Drag-and-drop component placement
  • Device-specific layouts (desktop, tablet, phone)
  • Dynamic visibility rules (show components conditionally)
  • Support for custom Lightning Web Components
  • Can override standard page layouts

Schema Builder: Visual tool for designing data models:

  • Drag-and-drop object and field creation
  • Visual relationship mapping
  • Real-time schema visualization
  • Useful for documentation and planning

Enterprise considerations:

  • Plan record types during data modeling, not as an afterthought
  • Record type changes in production require careful migration (existing records)
  • Page layout changes are metadata — deploy via change sets or SFDX
  • Lightning Record Pages offer more flexibility than classic page layouts
  • Consider record type impacts on reporting and dashboards

💻 Code Example

codeTap to expand ⛶
1// Working with Record Types in Apex
2
3public class RecordTypeUtils {
4
5 // 1. Get Record Type Id by Developer Name (best practice)
6 public static Id getRecordTypeId(String objectName, String devName) {
7 // Use Schema.SObjectType for reliable, cache-friendly access
8 return Schema.getGlobalDescribe()
9 .get(objectName)
10 .getDescribe()
11 .getRecordTypeInfosByDeveloperName()
12 .get(devName)
13 .getRecordTypeId();
14 }
15
16 // 2. Create records with specific Record Types
17 public static void createCases() {
18 Id supportRT = getRecordTypeId('Case', 'Support_Case');
19 Id bugRT = getRecordTypeId('Case', 'Bug_Report');
20
21 List<Case> cases = new List<Case>{
22 new Case(
23 Subject = 'Login Issue',
24 RecordTypeId = supportRT,
25 Priority = 'Medium',
26 Status = 'New'
27 ),
28 new Case(
29 Subject = 'Button not working',
30 RecordTypeId = bugRT,
31 Priority = 'High',
32 Status = 'Open' // Different picklist for bug reports
33 )
34 };
35 insert cases;
36 }
37
38 // 3. Query and filter by Record Type
39 public static List<Case> getCasesByType(String recordTypeName) {
40 return [
41 SELECT Id, Subject, Status, Priority, RecordType.Name
42 FROM Case
43 WHERE RecordType.DeveloperName = :recordTypeName
44 AND Status != 'Closed'
45 ORDER BY CreatedDate DESC
46 ];
47 }
48
49 // 4. Get available Record Types for current user
50 public static List<Schema.RecordTypeInfo> getAvailableRecordTypes(
51 String objectName
52 ) {
53 List<Schema.RecordTypeInfo> available = new List<Schema.RecordTypeInfo>();
54 Map<String, Schema.RecordTypeInfo> rtMap =
55 Schema.getGlobalDescribe()
56 .get(objectName)
57 .getDescribe()
58 .getRecordTypeInfosByDeveloperName();
59
60 for (Schema.RecordTypeInfo rti : rtMap.values()) {
61 if (rti.isAvailable() && !rti.isMaster()) {
62 available.add(rti);
63 }
64 }
65 return available;
66 }
67
68 // 5. Dynamically assign Record Type based on criteria
69 public static void assignRecordTypes(List<Case> cases) {
70 Id supportRT = getRecordTypeId('Case', 'Support_Case');
71 Id bugRT = getRecordTypeId('Case', 'Bug_Report');
72 Id featureRT = getRecordTypeId('Case', 'Feature_Request');
73
74 for (Case c : cases) {
75 if (c.Type == 'Bug') {
76 c.RecordTypeId = bugRT;
77 } else if (c.Type == 'Feature Request') {
78 c.RecordTypeId = featureRT;
79 } else {
80 c.RecordTypeId = supportRT;
81 }
82 }
83 }
84
85 // 6. Page Layout assignment query
86 // (useful for understanding which layout a user sees)
87 public static void checkLayoutAssignment() {
88 // In Apex, you can't directly query page layout assignments
89 // But you can use the Metadata API or Tooling API
90 // Instead, use Record Type + Profile combination logic
91
92 // Determine what record types the running user can access
93 Schema.DescribeSObjectResult caseDescribe = Case.SObjectType.getDescribe();
94 Map<Id, Schema.RecordTypeInfo> rtById = caseDescribe.getRecordTypeInfosById();
95
96 for (Id rtId : rtById.keySet()) {
97 Schema.RecordTypeInfo info = rtById.get(rtId);
98 System.debug(info.getName() + ' — Available: ' + info.isAvailable() +
99 ', Default: ' + info.isDefaultRecordTypeMapping());
100 }
101 }
102}

🏋️ Practice Exercise

Record Type & Layout Exercises:

  1. Create 3 Record Types for the Case object: Support, Bug, Feature Request — each with different picklist values
  2. Create different Page Layouts for each Record Type with different field arrangements
  3. Build a Lightning Record Page with dynamic visibility (show components based on record type)
  4. Write Apex that creates records with specific Record Types using Developer Name (not hard-coded IDs)
  5. Write a SOQL query that groups cases by RecordType.Name and counts them
  6. Design a migration script that changes Record Types for 50,000 existing records
  7. Create a formula field that displays different text based on the Record Type
  8. Test what happens when a user tries to create a record with a Record Type not assigned to their Profile
  9. Build a complete Lightning page with custom LWC components that change based on Record Type
  10. Document all Record Types in your org and their Page Layout assignments

⚠️ Common Mistakes

  • Hard-coding Record Type IDs — IDs differ between sandbox and production. Always use DeveloperName to look up IDs at runtime

  • Confusing Page Layout field visibility with Field-Level Security — Page Layouts only affect UI display; FLS controls actual read/write access to the field at the API level

  • Creating too many Record Types — each one requires its own Page Layout, picklist value set, and testing. Start with 2-3 max per object

  • Not considering Record Type impact on data migration — when importing data, you must specify RecordTypeId or the default is used, which may not be correct

  • Forgetting that Record Types are assigned to Profiles — if you add a new Record Type, users won't see it until you assign it to their Profile

💼 Interview Questions

🎤 Mock Interview

Mock interview is powered by AI for Record Types, Page Layouts & Schema. Login to unlock this feature.