어느 Salesforce Developer의 개발 성장기

Developer Console 기본편(4)-Execute SOQL and SOSL Queries 본문

Salesforce/Developer Tools

Developer Console 기본편(4)-Execute SOQL and SOSL Queries

Developer_Foryou 2020. 3. 9. 20:01

Execute soql queries

SQOL?

  • SOQL은 Salesforce Object Query Language의 약자.
  • SOQL을 사용하여 조직의 데이터베이스에 저장된 정보를 읽을 수 있다.
  • SOQL은 구문 적으로 SQL (Structured Query Language)과 유사하다.

ex. SOQL Query 실습
Debug | Open Execute Anonymous Window에서 아래 코드를 실행한다.

// Add first contact and related details
Contact contact1 = new Contact(
   Firstname='Quentin',
   Lastname='Foam',
   Phone='(415)555-1212',
   Department= 'Specialty Crisis Management',
   Title='Control Engineer - Specialty - Solar Arrays',
   Email='qfoam@trailhead.com');
insert contact1;
// Add second contact and related details
Contact contact2 = new Contact(
   Firstname='Vega',
   Lastname='North',
   Phone='(416)556-1312',
   Department= 'Specialty Crisis Management',
   Title='Control Engineer - Specialty - Propulsion',
   Email='vnorth@trailhead.com');
insert contact2;
// Add third contact and related details
Contact contact3 = new Contact(
   Firstname='Palma',
   Lastname='Sunrise',
   Phone='(554)623-1212',
   Department= 'Specialty Crisis Management',
   Title='Control Engineer - Specialty - Radiators',
   Email='psunrise@trailhead.com');
insert contact3;

방법1. Developer Console 하단의 Query Editor tab에 아래 코드를 실행한다.

SELECT Name, Phone, Email, Title FROM Contact
WHERE (Department = 'Specialty Crisis Management')

방법2. Debug | Open Execute Anonymous Window에서 코드를 활용하여 확인할 수 있다.

Contact[] theseContacts = [SELECT Name, Phone, Email, Description FROM Contact
                           WHERE (Department='Specialty Crisis Management')
                           ORDER BY Name];
// Log a count of how many contacts were found
System.debug(theseContacts.size() + ' contact(s) returned.');
// Log all values in the array of contacts
System.debug(theseContacts);

Execute SOSL Searchs

SOSL?

  • SOSL (Salesforce Object Search Language)은 레코드에서 텍스트 검색을 수행하는 언어이다.
  • SOQL과 달리 SOSL은 여러 유형의 객체를 동시에 쿼리 할 수 있다.
  • SOSL은 단어 일치를 사용하여 필드를 일치시킬 수 있지만 SOQL은 정확한 구문을 필요로한다.
  • ex.
    "Crisis"라는 단어를 사용하여 연락처 레코드에 대한 SOSL 검색을 실행하면 검색은 모든 연락처 필드를 살펴보고 그 단어가 들어있는 레코드를 반환한다. 그러나 SOQL 쿼리에서 같은 것을 시도하면 검색 할 필드와 검색 할 전체 단어 또는 구를 지정해야한다. 또한 LIKE 또는 wildcard를 사용하여 SOQL 또는 SOSL 검색 범위를 좁힐 수 있다.

ex.
방법1. Developer Console 하단의 Query Editor tab에 아래 코드를 실행한다.

FIND {Crisis} IN ALL FIELDS RETURNING Contact(FirstName, LastName, Phone, Email, Title)
image.png

방법2. Debug | Open Execute Anonymous Window에서 코드를 활용하여 확인할 수 있다.

List<List<sObject>> searchList = [FIND 'Crisis' IN ALL FIELDS 
                                  RETURNING Contact(FirstName, LastName,
                                  Phone, Email, Description)];
Contact[] searchContacts = (Contact[])searchList[0];
System.debug('Found the following contacts:');
for (Contact c : searchContacts) {
   System.debug(c.LastName + ', ' + c.FirstName);
}

 

 

Comments