binxie
2023-06-26 1b3fb93f787b8b546a307bf063183f5295d183f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
public with sharing class SampleLookupController {
    private final static Integer MAX_RESULTS = 5;
 
    @AuraEnabled(cacheable=true scope='global')
    public static List<LookupSearchResult> search(String searchTerm, List<String> selectedIds) {
        // Prepare query parameters
        searchTerm += '*';
 
        // Execute search query
        // List<List<SObject>> searchResults = [
        //     FIND :searchTerm
        //     IN ALL FIELDS
        //     RETURNING
        //         Account(Id, Name, BillingCity WHERE id NOT IN :selectedIds),
        //         Opportunity(Id, Name, StageName WHERE id NOT IN :selectedIds)
        //     LIMIT :MAX_RESULTS
        // ];
 
        // Prepare results
        List<LookupSearchResult> results = new List<LookupSearchResult>();
 
        // Extract Accounts & convert them into LookupSearchResult
        String accountIcon = 'standard:account';
        //Account[] accounts = (List<Account>) searchResults[0];
        Account[] accounts = [select id,Name,CreatedDate,BillingCity from Account limit 5];
        for (Account account : accounts) {
            String subtitle = account.BillingCity == null ? 'Account' : 'Account • ' + account.BillingCity;
            results.add(new LookupSearchResult(account.Id, 'Account', accountIcon, account.Name, subtitle));
        }
 
        // Extract Opportunities & convert them into LookupSearchResult
        // String opptyIcon = 'standard:opportunity';
        // Opportunity[] opptys = (List<Opportunity>) searchResults[1];
        // for (Opportunity oppty : opptys) {
        //     results.add(
        //         new LookupSearchResult(
        //             oppty.Id,
        //             'Opportunity',
        //             opptyIcon,
        //             oppty.Name,
        //             'Opportunity • ' + oppty.StageName
        //         )
        //     );
        // }
 
        // Optionnaly sort all results on title
        results.sort();
        System.debug('results = ' + results);
        return results;
    }
 
    @AuraEnabled(cacheable=true scope='global')
    public static List<LookupSearchResult> getRecentlyViewed() {
        List<LookupSearchResult> results = new List<LookupSearchResult>();
        // Get recently viewed records of type Account or Opportunity
        List<RecentlyViewed> recentRecords = [
            SELECT Id, Name, Type
            FROM RecentlyViewed
            WHERE Type = 'Account' OR Type = 'Opportunity'
            ORDER BY LastViewedDate DESC
            LIMIT 5
        ];
        // Convert recent records into LookupSearchResult
        for (RecentlyViewed recentRecord : recentRecords) {
            if (recentRecord.Type == 'Account') {
                results.add(
                    new LookupSearchResult(
                        recentRecord.Id,
                        'Account',
                        'standard:account',
                        recentRecord.Name,
                        'Account • ' + recentRecord.Name
                    )
                );
            } else {
                results.add(
                    new LookupSearchResult(
                        recentRecord.Id,
                        'Opportunity',
                        'standard:opportunity',
                        recentRecord.Name,
                        'Opportunity • ' + recentRecord.Name
                    )
                );
            }
        }
        return results;
    }
}