-1

How to pass the value for following test class

public static Result[] lookup(String searchString, String sObjectAPIName, String whereClause, String whereClauseType , String whereClauseValue, String displayField )
{
    System.debug('search string = ' + searchString);

    // Sanitze the input
    String sanitizedSearchString = String.escapeSingleQuotes(searchString);
    String sanitizedSObjectAPIName = String.escapeSingleQuotes(sObjectAPIName);
    String sanitizeWhereClause = '';
    String sanitizedDisplayField = '';

    List<String> listDisplayField = new List<String>();


    //
    if(whereClause != ''){

        if(whereClauseValue != ''){

            String[] listWhereClause = whereClause.split(',');
            String[] listWhereClauseType = whereClauseType.split(',');
            String[] listWhereClauseValue = whereClauseValue.split(',');


            sanitizeWhereClause = ' WHERE ' + buildWhereClause(listWhereClause,listWhereClauseType,listWhereClauseValue);    
        }

    }
    if(displayField != ''){
        sanitizedDisplayField = String.escapeSingleQuotes(displayField);
        listDisplayField = sanitizedDisplayField.split(',');
        sanitizedDisplayField = ', ' + sanitizedDisplayField;

    }


    List<Result> results = new List<Result>();

    // Build our SOSL query
    String searchQuery = 'FIND \'' + sanitizedSearchString + '*\' IN ALL FIELDS RETURNING ' + sanitizedSObjectAPIName + ' (id,name '+ sanitizedDisplayField + sanitizeWhereClause +' ORDER BY Name) Limit 50'; 

    system.debug('SEARCH QUERY : ' + searchQuery);

    // Execute the Query
    List<List<SObject>> searchList = search.query(searchQuery);

    // Create a list of matches to return
    for (SObject so : searchList[0])
    {

        //results.add(new Result((String)so.get('Name') + buildDisplayField(so,listDisplayField), so.Id));
        results.add(new Result((String)so.get('Name') + buildDisplayField(so,listDisplayField), so.Id, so));
    }

    System.debug('RESULT ' + results);
    return results;
}

Test class:

 public static testmethod void  LookupSObjectController(){
    List<String> listDisplayField=new List<String>();
    listDisplayField.add('s');
    listDisplayField.add('m');
    sObject sobj1 = new Account(Name='Trailhead');
     LookupSObjectController.lookup('SearchString','sobj1','select id,name from account','Where caluse','whereClauseValue','displayField');
}

In My case this Query is Building:

 FIND 'SearchString*' IN ALL FIELDS RETURNING sobj1 (id,name , displayField WHERE  ORDER BY Name) Limit 50

Test class gives following error:

    Unexpected token 'By'

After trying this code:

 LookupSObjectController.lookup('SearchString','sobj1__C','','','','');

I got this error:

System.QueryException: sObject type 'sobj1__c' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names.

Please help me on these test class

5
  • Did you try removing WHERE clause ?
    – Mr.Frodo
    Sep 11 '18 at 9:57
  • i have tried@Frodo. but its failing!!!! Sep 11 '18 at 9:58
  • This query FIND {ab*} IN ALL FIELDS RETURNING Contact (id, name ORDER BY Name) Limit 50 working fine for me. Braces {} are only applied when you query on developer console. So i think after removing Where it should work in your case.
    – Mr.Frodo
    Sep 11 '18 at 10:19
  • I have tried above code,it shows error Sep 11 '18 at 10:34
  • i have tried below line of code. LookupSObjectController.lookup('','Office__c','','','',''); it works!!!! Sep 11 '18 at 11:24
0

In My case this Query is Building:

FIND 'SearchString*' IN ALL FIELDS RETURNING sobj1 (id,name , displayField WHERE ORDER BY Name) Limit 50

notice that you have a WHERE key word with no clause right after.

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.