function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jonathan Wolff 7Jonathan Wolff 7 

Rewrite Component with where-clause and String.join

Hello, I have an apex class in which I change the soql with picklist fields from component. I would like to change the style of the class so it is with a where clause and a string join. Right now it is like that:
 
public with sharing class MediathekSearchController {
    

    @AuraEnabled(cacheable=true)
    public static List<List<sObject>> getSearchResult(String searchKey, String TypValue, String ZielgruppeValue, String ThemaValue){
        String findStr = '*'+searchKey+'*';
        
        Boolean isEmptySearch = String.isEmpty(searchKey);
        
        
        
        system.debug('searchKey ' + searchKey);
        system.debug('findStr ' + findStr);
        system.debug('KategorieValue' + TypValue);   
        system.debug('ZielgruppeValue' + ZielgruppeValue);
        system.debug('ThemaValue' + ThemaValue);
        
        
        List<List<sObject>> searchResult;
       
             if(ThemaValue == 'Alle' && ZielgruppeValue == 'Alle' && TypValue == 'Alle'){
                						searchResult = [FIND :findStr IN ALL FIELDS 
                                        RETURNING Mediathek__c (Id, Mediathek_ID_Long__c,  Thema__c, ContentDocumentID__c, ContentVersionId__c, Thumbnail_Image__c,Thumbnail_Preview2__c, Name, Bezeichnung__c, Typ__c, Zielgruppe__c, Umfang__c, Bezeichnung_Link__c, Bezeichnung_Search__c Where Inaktiv__c=false)];
             } 
        
    		else if(ThemaValue == 'Alle' && ZielgruppeValue == 'Alle' && TypValue != 'Alle'){ 
                        searchResult = [FIND :findStr IN ALL FIELDS 
                                        RETURNING Mediathek__c (Id,  Thema__c, Mediathek_ID_Long__c, Name, Bezeichnung__c, ContentDocumentID__c, Thumbnail_Image__c, Typ__c, Zielgruppe__c, Umfang__c, Bezeichnung_Link__c, Bezeichnung_Search__c
                                                                WHERE Typ__c=:TypValue AND Inaktiv__c=false)];
             }
        
        	else if(ThemaValue == 'Alle' && ZielgruppeValue != 'Alle' && TypValue == 'Alle'){ 
                        searchResult = [FIND :findStr IN ALL FIELDS 
                                        RETURNING Mediathek__c (Id,  Thema__c, Mediathek_ID_Long__c, Name, Bezeichnung__c, Typ__c, ContentDocumentID__c, Thumbnail_Image__c, Zielgruppe__c, Umfang__c, Bezeichnung_Link__c, Bezeichnung_Search__c
                                                                WHERE Zielgruppe__c=:ZielgruppeValue AND Inaktiv__c=false)];
             }
        
        	else if(ThemaValue != 'Alle' && ZielgruppeValue == 'Alle' && TypValue == 'Alle'){ 
                        searchResult = [FIND :findStr IN ALL FIELDS 
                                        RETURNING Mediathek__c (Id,  Thema__c, Name, Mediathek_ID_Long__c, Bezeichnung__c, Typ__c, ContentDocumentID__c, Zielgruppe__c, Umfang__c, Bezeichnung_Link__c, Bezeichnung_Search__c
                                                                WHERE Thema__c=:ThemaValue AND Inaktiv__c=false)];
             }
        
        	else if(ThemaValue == 'Alle' && ZielgruppeValue != 'Alle' && TypValue != 'Alle'){ 
                        searchResult = [FIND :findStr IN ALL FIELDS 
                                        RETURNING Mediathek__c (Id,  Thema__c, Name, Mediathek_ID_Long__c, Bezeichnung__c, Typ__c, ContentDocumentID__c, Zielgruppe__c, Umfang__c, Bezeichnung_Link__c, Bezeichnung_Search__c
                                                                WHERE Typ__c=:TypValue AND Zielgruppe__c=:ZielgruppeValue AND Inaktiv__c=false)];
             }
                    
                    
             else if(ThemaValue != 'Alle' && ZielgruppeValue != 'Alle' && TypValue == 'Alle'){ 
                        searchResult = [FIND :findStr IN ALL FIELDS 
                                        RETURNING Mediathek__c (Id,  Thema__c, Name, Mediathek_ID_Long__c, Bezeichnung__c, Typ__c, ContentDocumentID__c, Zielgruppe__c, Umfang__c, Bezeichnung_Link__c, Bezeichnung_Search__c
                                                                WHERE Thema__c=:ThemaValue AND Zielgruppe__c=:ZielgruppeValue AND Inaktiv__c=false)];
             }          
              else if(ThemaValue != 'Alle' && ZielgruppeValue == 'Alle' && TypValue != 'Alle'){ 
                        searchResult = [FIND :findStr IN ALL FIELDS 
                                        RETURNING Mediathek__c (Id,  Thema__c, Name, Mediathek_ID_Long__c, Bezeichnung__c, ContentDocumentID__c, Typ__c, Zielgruppe__c, Umfang__c, Bezeichnung_Link__c, Bezeichnung_Search__c
                                                                WHERE Thema__c=:ThemaValue AND Typ__c=:TypValue AND Inaktiv__c=false)];
                                   
                    }
        
              else if(ThemaValue != 'Alle' && ZielgruppeValue != 'Alle' && TypValue != 'Alle'){ 
                        searchResult = [FIND :findStr IN ALL FIELDS 
                                        RETURNING Mediathek__c (Id,  Thema__c, Mediathek_ID_Long__c, Name, Bezeichnung__c, ContentDocumentID__c, Typ__c, Zielgruppe__c, Umfang__c, Bezeichnung_Link__c, Bezeichnung_Search__c
                                                                WHERE Thema__c=:ThemaValue AND Typ__c=:TypValue AND Zielgruppe__c=:ZielgruppeValue AND Inaktiv__c=false)];
                    
                } 
            
              
       
            return searchResult;
        }

The style of how i would like it is like that:
 
String SelectClause = 'SELECT Id, OwnerId, Name, BillingCity, NumberOfEmployees, Owner.Name, (SELECT LastModifiedDate FROM Opportunities ORDER BY LastModifiedDate DESC Limit 1) FROM Account';
String SelectClause = 'SELECT Id, OwnerId, Name, BillingCity, NumberOfEmployees, Owner.Name, (SELECT LastModifiedDate FROM Opportunities ORDER BY LastModifiedDate DESC Limit 1) FROM Account';   
String LimitClause = ' ORDER BY Name LIMIT 1000';    
    String CountSelectClause = 'SELECT Count() FROM Account';
    String CountLimitClause = ' LIMIT 1000';
    List <String> WhereClauses = new List <String>();
    IF (!isEmptyStand) {
        IF (SelectedStand == 'GREEN') {
            WhereClauses.Add('Id IN (SELECT AccountId FROM Opportunity WHERE LastModifiedDate = LAST_N_DAYS:60)');
        } 
        ELSE IF (SelectedStand == 'YELLOW') {
            WhereClauses.Add('Id IN (SELECT AccountId FROM Opportunity WHERE LastModifiedDate < LAST_N_DAYS:60 AND LastModifiedDate = LAST_N_DAYS:180)');
        }
        ELSE IF (SelectedStand == 'RED') {
            WhereClauses.Add('Id IN (SELECT AccountId FROM Opportunity WHERE LastModifiedDate < LAST_N_DAYS:180)');
        }  
        ELSE IF (SelectedStand == 'LEER') {
            WhereClauses.Add('Id NOT IN (SELECT AccountId FROM Opportunity)');
        }       
        ELSE IF (SelectedStand == 'MIN1') {
            WhereClauses.Add('Id IN (SELECT AccountId FROM Opportunity)');
        }               
    }           IF (!isEmptySearch) {
        WhereClauses.Add('Name LIKE \'' + SearchStringConvert + '\'');
    }
    IF (!isEmptyStatus) {
        WhereClauses.Add('Status__c = \'' + SelectedStatus + '\'');
    }
    IF (!isEmptyUser && SelectedUserId.contains('Team')) { 
        WhereClauses.add('Team_Unternehmen__c = \'' + SelectedUserId + '\'');
    } 
    IF (!isEmptyUser && !SelectedUserId.contains('Team')) {
        WhereClauses.Add('OwnerId = \'' + SelectedUserId + '\'');
    }
    String WhereClause = ' WHERE ' + String.join(WhereClauses, ' AND ');
    String SQL = SelectClause + WhereClause + LimitClause;
    String SQLCount = CountSelectClause + WhereClause + CountLimitClause;
    System.Debug('>>>> the value of SQL is ' + SQL);
    System.Debug('>>>> the value of SQL is ' + SQLCount);    
    List <Account> Acc = Database.query(SQL);

 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Jonathan,

Which line you are getting an issue?

Thanks!!