/* -------------------------------------------------- Strike by Appiphony Version: 0.10.0 Website: http://www.lightningstrike.io GitHub: https://github.com/appiphony/Strike-Components License: BSD 3-Clause License -------------------------------------------------- */ public with sharing class strike_lookupController { @AuraEnabled public static String getRecentRecords(String jsonString) { strike_responseData responseData = new strike_responseData(); try { Map jsonMap = (Map)JSON.deserializeUntyped(jsonString); Map recentlyViewedMap = new Map([SELECT Id FROM RecentlyViewed WHERE Type = :((String)jsonMap.get('object'))]); List idList = new List(recentlyViewedMap.keySet()); if (idList.size() > 0) { String filter = 'Id IN (\'' + String.join(idList, '\',\'') + '\')'; if (strike_lookupController.fieldNotEmpty(jsonMap, 'filter')) { filter += ' AND (' + jsonMap.get('filter') + ')'; } jsonMap.put('filter', filter); responseData.results = strike_lookupController.getData(jsonMap); } else { responseData.results = new Map{ 'data' => new List(), 'searchTerm' => '' }; } } catch (Exception e) { responseData.addError(e.getMessage()); } return responseData.getJsonString(); } @AuraEnabled public static String getRecordLabel(String jsonString) { strike_responseData responseData = new strike_responseData(); try { Map jsonMap = (Map)JSON.deserializeUntyped(jsonString); String obj = (String)jsonMap.get('object'); String objectLabel = Schema.describeSObjects(new List{obj})[0].getLabel(); responseData.results.put('objectLabel', objectLabel); } catch (Exception e) { responseData.addError(e.getMessage()); } return responseData.getJsonString(); } @AuraEnabled public static String getRecords(String jsonString) { strike_responseData responseData = new strike_responseData(); try { Map jsonMap = (Map)JSON.deserializeUntyped(jsonString); responseData.results = strike_lookupController.getData(jsonMap); } catch (Exception e) { responseData.addError(e.getMessage()); } return responseData.getJsonString(); } private static Map getData(Map jsonMap) { List> data = new List>(); String objType = String.escapeSingleQuotes((String)jsonMap.get('object')); String query = strike_lookupController.getQuery(jsonMap); String searchField = String.escapeSingleQuotes((String)jsonMap.get('searchField')); String searchTerm = ''; String subtitleField; String subTitleFormat = ''; system.debug('搜索框的SQL语句:'+query); if (strike_lookupController.fieldNotEmpty(jsonMap, 'subtitleField')) { subtitleField = String.escapeSingleQuotes((String)jsonMap.get('subtitleField')); } if (strike_lookupController.fieldNotEmpty(jsonMap, 'searchTerm')) { searchTerm = String.escapeSingleQuotes((String)jsonMap.get('searchTerm')); } if (strike_lookupController.fieldNotEmpty(jsonMap, 'subTitleFormat')) { subTitleFormat = String.escapeSingleQuotes((String)jsonMap.get('subTitleFormat')); } System.debug(query); if (String.isEmpty(subtitleField)) { for (sObject obj : Database.query(query)) { data.add(new Map{ 'label' => strike_lookupController.getValue(obj, objType, searchField), 'value' => obj.get('Id') }); } } else { List subtitleFieldList = subtitleField.split(','); for (sObject obj : Database.query(query)) { List titleList = new List(); for (String field : subtitleFieldList) { titleList.add(strike_lookupController.getValue(obj, objType, field)); } data.add(new Map{ 'label' => strike_lookupController.getValue(obj, objType, searchField), 'sublabel' => String.format(subTitleFormat, titleList), 'value' => obj.get('Id') }); } } return new Map{ 'data' => data, 'searchTerm' => searchTerm }; } private static String getQuery(Map jsonMap) { Set queryFields = new Set{'Id'}; List filters = new List(); List orders = new List(); String query; String obj = String.escapeSingleQuotes((String)jsonMap.get('object')); String subtitleField; if (strike_lookupController.fieldNotEmpty(jsonMap, 'subtitleField')) { subtitleField = String.escapeSingleQuotes((String)jsonMap.get('subtitleField')); queryFields.add(subtitleField); } if (strike_lookupController.fieldNotEmpty(jsonMap, 'searchField')) { queryFields.add(String.escapeSingleQuotes((String)jsonMap.get('searchField'))); if (strike_lookupController.fieldNotEmpty(jsonMap, 'searchTerm')) { String searchField = String.escapeSingleQuotes((String)jsonMap.get('searchField')); String searchTerm = String.escapeSingleQuotes((String)jsonMap.get('searchTerm')); filters.add(searchField + ' LIKE \'%' + searchTerm + '%\''); } } if (strike_lookupController.fieldNotEmpty(jsonMap, 'filter')) { filters.add('(' + (String)jsonMap.get('filter') + ')'); } if (strike_lookupController.fieldNotEmpty(jsonMap, 'order')) { orders.add(String.escapeSingleQuotes((String)jsonMap.get('order'))); } query = 'SELECT ' + String.join(new List(queryFields), ', '); query += ' FROM ' + obj; if (filters.size() > 0) { query += ' WHERE ' + String.join(filters, ' AND '); } if (orders.size() > 0) { query += ' ORDER BY ' + String.join(orders, ', '); } if (strike_lookupController.fieldNotEmpty(jsonMap, 'limit')) { query += ' LIMIT ' + String.escapeSingleQuotes((String)jsonMap.get('limit')); } return query; } private static Boolean fieldNotEmpty(Map jsonMap, String field) { return jsonMap.containsKey(field) && !String.isEmpty((String)jsonMap.get(field)); } private static String getValue(SObject obj, String objectType, String field) { List fieldPath = field.split('[.]'); Object label = strike_utilities.convertObjectToMap(obj); Map fieldMap = Schema.getGlobalDescribe().get(objectType).getDescribe().fields.getMap(); for (String fieldName : fieldPath) { //fieldName = fieldName.replaceAll('__r$', '__c'); if (label == null) { return ''; } label = ((Map)label).get(fieldName); if (fieldName.contains('__r')) { continue; } if (label == null) { return ''; } if (fieldMap.containsKey(fieldName + 'Id')) { fieldName = fieldName + 'Id'; } Schema.DescribeFieldResult fieldDescribe = fieldMap.get(fieldName).getDescribe(); String fieldType = String.valueOf(fieldDescribe.getType()).toUpperCase(); if (fieldType == 'REFERENCE') { fieldMap = Schema.getGlobalDescribe().get(String.valueOf(fieldDescribe.getReferenceTo().get(0))).getDescribe().fields.getMap(); } else if (fieldType == 'ADDRESS') { List addressComponents = new List(); Map addr = (Map)label; if (addr.containsKey('street') && addr.get('street') != null) { addressComponents.add((String)addr.get('street')); } if (addr.containsKey('city') && addr.get('city') != null) { addressComponents.add((String)addr.get('city')); } if (addr.containsKey('state') && addr.get('state') != null) { addressComponents.add((String)addr.get('state')); } if (addr.containsKey('country') && addr.get('country') != null) { addressComponents.add((String)addr.get('country')); } if (addr.containsKey('postalCode') &&addr.get('postalCode') != null) { addressComponents.add((String)addr.get('postalCode')); } // change later for user formatting? label = String.join(addressComponents, ', '); } } return String.valueOf(label); } } /* -------------------------------------------------- Copyright 2017 Appiphony, LLC Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------- */