0

I want to check the surname matching lastname and Pcode matching MailingPostalCode in the query of SOQL.

public class ContactSearch 
{
    public static list<contact> searchForContacts(String Surname,String PCode)
    {   
        list<Contact> Listquery = [select ID, Name, LastName, MailingPostalCode from contact 
                                  where lastName IN : Surname];
    }

}
2
  • why do you use IN clause? do you have any issues faced with using simple = comparison ?
    – highfive
    Dec 18, 2014 at 6:23
  • Yeah I was not adding colon but now added Dec 18, 2014 at 6:42

3 Answers 3

0

If you need to extract the records exactly matching the Surname and PCode simply apply a filter for them.

public class ContactSearch 
{
    public static list<contact> searchForContacts(String Surname,String PCode)
    {   
        list<Contact> listQuery = [select ID, Name, LastName, MailingPostalCode from contact 
                                  where lastName =: Surname AND MailingPostalCode=: PCode];

        return listQuery;
    }

}
2
  • How to retrieve the result ?? Dec 18, 2014 at 8:13
  • What you mean? query result will be assigned into listQuery you can return in as in my edited answer
    – highfive
    Dec 18, 2014 at 8:26
0

To search using wildcard LIKE operator use something like

list<Contact> Listquery = 'SELECT ID, Name, LastName, MailingPostalCode FROM contact WHERE lastName LIKE ' + Surname + '%' AND MailingPostalCode =: ' + Pcode

Or you could use exact match SurName by just changing one line, as given below, from above query: WHERE lastName =:' + Surname

2
  • Yeah I changed but how we can get the result of list as output ? Is it the correct way ? for(contact c : listquery) { system.debug('Answer'+c.get(c.lastname));} Dec 18, 2014 at 6:43
  • Yes you can do that or more simply you could use for(contact c : [SELECT ID ... FROM contact WHERE .... ] ). using the same query you used to build ListQuery variable in for loop. I hope it helps.
    – Mahmood
    Dec 18, 2014 at 6:52
0

I think you have compare two fields LastName & Surname for matching data right ?

List<contact> Listquery = database.query('select ID, Name, LastName, MailingPostalCode from contact where lastName =: Surname');

I think you getting error for IN operator just remove it and put =:

Try This in developer console

List<String> surname = new List<String>();
surname.add('Rogers');
List<String> Pcode= new List<String>();
Pcode.add('27215');
List<contact> C =[select ID, Name, LastName, MailingPostalCode from contact where lastName IN : Surname or MailingPostalCode In : PCode];
system.debug(C);

Might be this useful for you if i am wrong then correct me.

7
  • I changed that from IN to =: but now I am not getting my listQuery after the following code: list<Contact> Listquery = [select ID, Name, LastName, MailingPostalCode from contact where lastName =: Surname AND MailingPostalCode =: PCode]; I am not getting any output after this input ContactSearch.searchForContacts('Barat', '4y8p9o'); Dec 18, 2014 at 6:42
  • have you tried your query in developer console?? if not then please check... Dec 18, 2014 at 6:51
  • but how should I put a condition of "PCode" and "Surname" in developer console ??? Dec 18, 2014 at 6:57
  • Its an empty result ! Showing nothing Dec 18, 2014 at 7:07
  • use 'Rogers' as surname instead of 'Forbes'. because if you can see the where clause then according to it the result is empty. if you are concern about your data into database then adjust where clause accordingly. Dec 18, 2014 at 7:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .