Script-thrown exception in salesforce Apex

apexdynamic-sosl

Hi I am trying to make a dynamic SOSL on the basis of certain condition, but getting the error as script thrown exception.

below is my code :

List<List<ABC__c>> list1 = new List<List<ABC__c>>();
String searchQuery = makeDynamicQuery(textA,textB);
list1 = search.query(searchQuery);


public static String makeDynamicQuery(String textA, String textB) {
        String makeQuery = null;
        String sampleSearchab = textA +'*';
        if(sampleSearchab.length()>1 && textB!=null ) {
            if(textB == 'All') {
                makeQuery = 'FIND\'sampleSearchab\'IN ALL FIELDS RETURNING ABC__c(Field1, '+
                    'Field2,Field3,Field13'+
                    'WHERE Field2 = true ORDER BY Field13)';
            }
            if(textB == 'True') {
                makeQuery = 'FIND\'sampleSearchab\'IN ALL FIELDS RETURNING ABC__c(Field1, '+
                    'Field2,Field3,Field13'+
                    'WHERE Field2 = true AND Field3=true ORDER BY Field13)';
            }
        }
        return makeQuery;
    }

Error : Script thrown exception…
kindly help me out:

Best Answer

The query is malformed. Here is the correct way - added a space before WHERE and sampleSearchab concatenated the right way.

if(textB == 'All') {
    makeQuery = 'FIND \'' + sampleSearchab + '\' IN ALL FIELDS RETURNING Account(field1, '+
        'Field2,Field3,Field13'+
        ' WHERE Field2 = true ORDER BY Field13)';
}
Related Topic