1

I've made a search box in my lightning component to search for specific people in the database. However it seems like my like clause is not working correctly as it returns no guests.

I have debugged the attribute I am passing in and it is not null.

Query:

@AuraEnabled
public static List<Guest__c> getSpecificGuests(id eventId, string guestName) 
{
    List<Guest__c> Guests =  [SELECT Id, Guest_Name__c, Seat__c,
                              FROM Guest__c 
                              WHERE Booking__r.Event__c=:eventId 
                              AND Seat__c=:null 
                              AND Guest_Name__c LIKE : guestName];
    return Guests;
}
2

This is because the LIKE operator works with wildcards.

Expression is true if the value in the specified fieldName matches the characters of the text string in the specified value. The LIKE operator in SOQL and SOSL is similar to the LIKE operator in SQL; it provides a mechanism for matching partial text strings and includes support for wildcards.

  • The % and _ wildcards are supported for the LIKE operator.
  • The % wildcard matches zero or more characters.
  • The _ wildcard matches exactly one character.
  • The text string in the specified value must be enclosed in single quotes. The LIKE operator is supported for string fields only. The LIKE operator performs a case-insensitive match, unlike the case-sensitive matching in SQL. The LIKE operator in SOQL and SOSL supports escaping of special characters % or _. Don’t use the backslash character in a search except to escape a special character.

For example, the following query matches Appleton, Apple, and Appl, but not Bappl: SELECT AccountId, FirstName, lastname FROM Contact WHERE lastname LIKE 'appl%'

So in your code, it would be:

@AuraEnabled
public static List<Guest__c> getSpecificGuests(id eventId, string guestName) 
{
    guestName = '%' + guestName + '%';
    List<Guest__c> Guests =  [SELECT Id, Guest_Name__c, Seat__c,
                              FROM Guest__c 
                              WHERE Booking__r.Event__c=:eventId 
                              AND Seat__c=:null 
                              AND Guest_Name__c LIKE : guestName];
    return Guests;
}
| improve this answer | |
  • You're welcome ! Glad I could help. – Martin Lezer Oct 25 '18 at 9:37

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.