1

I'd like my LWC component to retrieve all records of an SObject type without having to create a custom AuraEnabled method using Lightning Data Services. Is this possible?

  • no one record at a time – User6670 Sep 5 '19 at 11:11
4

No.

LDS - lightning/uiRecordApi, lightning-record-form, lightning-record-edit-form and lightning-record-view-form - are all based for only 1 record .

For simplicity you can write custom apex utils method which accepts parameters like list of fields, object name, query filters etc which can return both SOQL or SOSL query results.

This can be a starting point for creating such method .

@AuraEnabled
public static Map<String, Object> fetchDataMap(Map<String, Object> params) {

    String objectName = params.containsKey('objectName') ? (String)params.get('objectName') : null;
    String fields = params.containsKey('fields') ? (String)params.get('fields') : null;
    String queryFilters = params.containsKey('queryFilters') ? (String)params.get('queryFilters') : null;
    String sortBy = params.containsKey('sortBy') ? (String)params.get('sortBy') : null;
    String queryType = params.containsKey('queryType') ? (String)params.get('queryType') : null;
    String soslSearchTerm = params.containsKey('soslSearchTerm') ? (String)params.get('soslSearchTerm') : null;
    Boolean sortAsc = params.containsKey('sortAsc') ? (Boolean)params.get('sortAsc') : false;
    Integer limitRecords = params.containsKey('limitRecords') ? Integer.valueOf(params.get('limitRecords')) : null;

    try{
        //Initial checks
        String limitRecordsStr = String.valueOf(Integer.valueOf(limitRecords));

        //Declare query string
        String query;

        //Query initialization for Soql and Sosl
        if(queryType == 'SOQL'){
            query = 'SELECT Id, ' + fields + ' FROM ' + objectName;
        }
        else if(queryType == 'SOSL') {
            query = 'Id, ' + fields;
        }

        //Adding filters
        if(String.isNotBlank(queryFilters)){
            query += ' WHERE ' + queryFilters;
        }

        //Adding order by and limit records
        if(String.isNotBlank(sortBy) && queryType == 'SOQL'){
            query += ' ORDER BY ' + sortBy + (sortAsc?' ASC ':' DESC ');
        }

        if(String.isNotBlank(limitRecordsStr)) {
            query += ' LIMIT ' + limitRecordsStr;
        }

        //Log the query before getting query results from database
        Map<String, Object> returnMap = new Map<String, Object>();
        List<sObject> sObjectsList = new List<sObject>();
        if(queryType == 'SOQL'){
            system.debug('query => '+query);
            sObjectsList = Database.query(query);
        }
        else if(queryType == 'SOSL') {
            query = 'FIND \'' + String.escapeSingleQuotes(soslSearchTerm) + '\' IN ALL FIELDS RETURNING ' + objectName + '(' + query + ')';
            system.debug('query => '+query);
            sObjectsList = Search.query(query)[0];
        }

        returnMap.put('records', sObjectsList);

        //Log the result
        system.debug('returnMap => '+returnMap);

        return returnMap;     
    }
    catch(Exception ex) {
        system.debug('Error => '+ex.getMessage());
        throw new AuraHandledException(ex.getMessage());
    }   
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.