You need to sign in to do that
Don't have an account?
Mark Mulholland 3
SOQL search using a list. Want to use the equals operator with an Apex variable
Hello all,
I am trying to create a simple query against Accounts and Contacts, trying to see if a contact has a selected Email address and the parent Account has a specific IATA number. I am getting into some basic API creation and am using files from a Developerforce conference from 2 years ago. I modified the code to use field specific to my org
The SOQL query returns values only if I use the LIKE operator and doesn't return any results if I use the equals operator. See below for what I mean
Because I used LIKE in the SOQL query I get results returned. However if I modify the code as you can see in the next fragment I get no results
However, if you do not use a List for the results and simply use a String, then you can use the equals operator.
Is this expected behaviour? and if so is there any way I can made the search more selective? or make it pseudo equals?
Thanks
I am trying to create a simple query against Accounts and Contacts, trying to see if a contact has a selected Email address and the parent Account has a specific IATA number. I am getting into some basic API creation and am using files from a Developerforce conference from 2 years ago. I modified the code to use field specific to my org
The SOQL query returns values only if I use the LIKE operator and doesn't return any results if I use the equals operator. See below for what I mean
String searchText = '%'+searchTerm1+'%'; List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c FROM Contact WHERE Account.IATA_Code__c LIKE :searchText]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search term.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that IATA Code, please search again.'; }
Because I used LIKE in the SOQL query I get results returned. However if I modify the code as you can see in the next fragment I get no results
String searchText = '%'+searchTerm1+'%'; List<Contact> searchResults = [SELECT Id, Name, Phone, Email, Account.IATA_Code__c FROM Contact WHERE Account.IATA_Code__c = :searchText]; if(searchResults != null && searchResults.size() > 0) { response.acctList = searchResults; response.status = 'Success'; response.message = searchResults.size() + ' Contacts were found that matched your search term.'; } else { response.status = 'Error'; response.message = 'No Contacts where found based on that IATA Code, please search again.'; }
However, if you do not use a List for the results and simply use a String, then you can use the equals operator.
Is this expected behaviour? and if so is there any way I can made the search more selective? or make it pseudo equals?
Thanks
Account Name1 :- Boom
Account Name2 :- Ka Boom
- LIKE '%Boom%' will return both accounts
- = 'Boom' will return only first acount
- = '%Boom%' will not return anything because there is no account with name %Boom%
= will only return exact matchLIKE is used for matching a string in the text which supports wild cards like % means anything before/after, "_" escaping a text etc.
All Answers
Account Name1 :- Boom
Account Name2 :- Ka Boom
- LIKE '%Boom%' will return both accounts
- = 'Boom' will return only first acount
- = '%Boom%' will not return anything because there is no account with name %Boom%
= will only return exact matchLIKE is used for matching a string in the text which supports wild cards like % means anything before/after, "_" escaping a text etc.
Mark
Please select best answer if that has solved your problem
Thank You