0

I have created a lightning component with 3 filters- one for searching listview, one from dropdown (picklist) and third by searching the name. I'm able to make it work by individual search but for combining have been asked to use dynamic SQL in apex by passing the string in apex class. So here's code is attached of class .But I don't know how to move forward.

public with sharing class ApexSearchForm {
    
    @AuraEnabled   
        public static List<Asset__c> fetchAccounts(String status, String v) {
            String a= 'Recently Viewed Assets';
            String b='All Assets';
            String c='Choose One';
            List<Asset__c> accList = new List<Asset__c>(); 
            if(String.isNotBlank(status)){
            accList = [SELECT Name, Status__c, Description__c, Executive_Sponsor__c, Documentation_Link__c,Video_Link__c,Source_Code_Link__c
                       FROM Asset__c 
                       WHERE Status__c = :status];
            }if(String.isBlank(status)){                 
                 accList = [SELECT Name, Status__c, Description__c, Executive_Sponsor__c, Documentation_Link__c, Video_Link__c,Source_Code_Link__c
                            FROM Asset__c LIMIT 100];
            }if(v == a){   
                accList = [SELECT Id, Name, Description__c, Submitted_Date__c, Documentation_Link__c, Source_Code_Link__c,Video_Link__c,Status__c
                           FROM Asset__c 
                           WHERE LastViewedDate != NULL
                           ORDER BY LastViewedDate DESC LIMIT 5];
                
            }else if(v==b){
                accList = [SELECT Id, Name, Description__c, Submitted_Date__c, Documentation_Link__c, Source_Code_Link__c,Video_Link__c,Status__c
                           FROM Asset__c 
                           ORDER BY Name  ASC ]; 
                                 
                
            }else if(v==c){
                accList = [SELECT Id, Name, Description__c, Submitted_Date__c, Documentation_Link__c, Source_Code_Link__c,Video_Link__c,Status__c
                           FROM Asset__c ];
            }else if(String.isNotBlank(status) && v==a){
                accList=[SELECT Id, Name, Description__c, Submitted_Date__c, Documentation_Link__c, Source_Code_Link__c,Video_Link__c,Status__c
                        FROM Asset__c 
                        WHERE Status__c = :status
                        ORDER BY LastViewedDate DESC];
            }
            return accList;
        }

       @AuraEnabled 
       public static Map<String, String> getStatusFieldValue(){
        Map<String, String> options = new Map<String, String>();        
        Schema.DescribeFieldResult fieldResult = Asset__c.Status__c.getDescribe();        
        List<Schema.PicklistEntry> pValues = fieldResult.getPicklistValues();
        for (Schema.PicklistEntry p: pValues) {            
            options.put(p.getValue(), p.getLabel());
        }
        return options;
    }
}
Sven Eberth
  • 3,057
  • 12
  • 24
  • 29

1 Answers1

0

Assuming that you pass correctly the parameters from frontend to the controller, you can do something like:

public static List<Asset__c> fetchAccounts(String status, String v) {
    String a= 'Recently Viewed Assets';
    String b='All Assets';
    String query = 'SELECT Name, Status__c, Description__c, Executive_Sponsor__c, Documentation_Link__c,Video_Link__c,Source_Code_Link__c FROM Asset__c';

    if(String.isNotBlank(status) || v != null){
        query += ' WHERE';
        if(String.isNotBlank(status)){
            query += ' Status__c = :status';
        }
            
        if(v != null){
            query += ' ORDER BY';
            if(v == a){
                query += ' LastViewedDate DESC';
                    if(String.isNotBlank(status)){
                        query += ' LIMIT 5';
                    }
            } else if(v == b){
                query += ' Name  ASC';
            }
        }
    } else {
        query += ' LIMIT 100';
    }

    List<Asset__c> accList = Database.query(query);
    return accList;
}