liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
public with sharing class SearchReportNameController {
 
    @AuraEnabled
    public static ResponseBodyLWC searchReportByName(String reportName) {
        String baseUrl = URL.getOrgDomainURL().toExternalForm();
        ResponseBodyLWC response = new ResponseBodyLWC();
        try {
            List<DataResponse> dataRespList = new List<DataResponse>();
            List<Id> reportIdList = new List<Id>();
            String sql = 'SELECT Id, Name, Description, OwnerId, CreatedById, FolderName, CreatedBy.Name, CreatedDate FROM Report WHERE ';
            List<RecentlyViewed> reportRecentlyList= [SELECT Id, Name FROM RecentlyViewed  WHERE Type ='Report'];
            for (RecentlyViewed reportRecently : reportRecentlyList) {
                reportIdList.add(reportRecently.Id);
            }
            if (String.isNotBlank(reportName)) {
                sql += ' Name like \'%' + reportName + '%\' ';
            } else {
                sql += 'Id IN :reportIdList ';
            }
            sql += ' LIMIT 200';
            System.debug('sql=====>' + sql);
            List<Report> reportList = DataBase.query(sql);
            for (Report re : reportList) {
                DataResponse dataRes = new DataResponse();
                dataRes.reportUrl = baseUrl + '/' + re.Id;
                dataRes.folderUrl = baseUrl + '/' + re.OwnerId;
                dataRes.createdbyUrl = baseUrl + '/' + re.CreatedById;
                dataRes.name = re.Name;
                dataRes.description = re.Description;
                dataRes.folderName = re.FolderName;
                dataRes.createdByName = re.CreatedBy.Name;
                dataRes.createdDate = re.CreatedDate;
                dataRespList.add(dataRes);
            }
            System.debug('dataRespList=====>' + dataRespList);
            response.entity = JSON.serialize(dataRespList);
            response.status = 'Success';
        } catch (Exception e) {
            response.status = 'fail';
            response.msg = e.getMessage() + '\n' + e.getLineNumber();
        }
        return response;
    }
 
    public class DataResponse {
        public String name {get; set;}
        public String reportUrl {get; set;}
        public String folderUrl {get; set;}
        public String createdbyUrl {get; set;}
        public String description {get; set;}
        public String folderName {get; set;}
        public String createdByName {get; set;}
        public DateTime createdDate {get; set;}
 
        public DataResponse() {
 
        }
    }
    
}