+ Start a Discussion
ThomasITThomasIT 

How use soql with apex?

Hi,

 

i'm a newbie with apex. (Sorry for my english) .I want to write a query (soql) but i don't know how start!

i need an apex class to use query? or what?

i had create a custom object named "phonebook" that have the following fields : Name, Surname, Telephone number.

Suppse that i want to retrive contacts with filde surname " Russel", how should  i do?

Tnx

Best Answer chosen by Admin (Salesforce Developers) 
MJ09MJ09

Your find() method isn't quite right.

 

1. You define the method as returning a single phonebook (should be phonebook__c, with "__c" if phonebook is a custom object) object, but then it returns a list of phonebook__c objects.

 

2. Your SOQL query is missing the "where" keyword.

 

Also (and this is more a matter of style and readability than strict correctness), try to use the same case when refering to variable names. For example, your code uses both allPhoneBookEntries and AllPhoneBooKEntries. Pick one and use it consistently.

 

This would be more correct:

 

 

static List<Phonebook__c> find() {
List<Phonebook__c> allPhoneBookEntries =
[select Name, Surname__c, Telephone_number__c
from Phonebook__c
where Name ='Russe'];
return allPhoneBookEntries;
}

Message Edited by MJ09 on 03-17-2009 08:15 AM

All Answers

micwamicwa

It would look like this:

 

List<phonebook__c> allPhoneBookEntries = [select Name, Surname__c, Telephone_number__c from phonebook__c where Name = 'Russel'];

 

 

 

ThomasITThomasIT

i write an apex class like this :

 

global class SearchContact

{

         

               static    phonebook find()

               {

 

                    List<phonebook__c> allPhoneBookEntries =[select Name, Surname__c, Telephone_number__c

                                                                                         from phonebook__c Name ='Russe' ];

                    return AllPhoneBooKEntries;

                }

}

 

That's rigth?

Now, 'd like to use this class like a webservice in order to retrieve date from outer the salesforce platform.

can i do it by the apex class above?

Tnx for reply

MJ09MJ09

Your find() method isn't quite right.

 

1. You define the method as returning a single phonebook (should be phonebook__c, with "__c" if phonebook is a custom object) object, but then it returns a list of phonebook__c objects.

 

2. Your SOQL query is missing the "where" keyword.

 

Also (and this is more a matter of style and readability than strict correctness), try to use the same case when refering to variable names. For example, your code uses both allPhoneBookEntries and AllPhoneBooKEntries. Pick one and use it consistently.

 

This would be more correct:

 

 

static List<Phonebook__c> find() {
List<Phonebook__c> allPhoneBookEntries =
[select Name, Surname__c, Telephone_number__c
from Phonebook__c
where Name ='Russe'];
return allPhoneBookEntries;
}

Message Edited by MJ09 on 03-17-2009 08:15 AM
This was selected as the best answer
ThomasITThomasIT
Run! :smileyhappy: Thanks for repling!