๋ฐ์ดํฐ๋ฅผ Apex์ ๋ฐํํ๋ SOQL ์์ฑ
Trail Together์ ํจ๊ป ์์๋ณด๊ธฐ
์ด ๋จ๊ณ๋ฅผ ์ ๋ฌธ๊ฐ์ ํจ๊ป ์งํํ๊ณ ์ถ์ผ์ ๊ฐ์? Trail Together ์๋ฆฌ์ฆ์ ์ผ๋ถ์ธ ์ด ๋น๋์ค๋ฅผ ์์ฒญํ์ธ์.
(์ด ์์์ 24๋ถ 15์ด๋ถํฐ ์์ํฉ๋๋ค. ๋จ๊ณ ์์ ๋ถ๋ถ์ผ๋ก ๋๋๋ ค ๋ค์ ์์ฒญํ๋ ค๋ ๊ฒฝ์ฐ ์ฐธ๊ณ ํ์ธ์.)
์ฟผ๋ฆฌ ๋ง๋ค๊ธฐ
์ ์ ๋ง๋ Recruiting Account(์ฑ์ฉ ๊ณ์ข) ๋ ์ฝ๋๋ฅผ ๊ฒ์ํ๋ SOQL ์ฟผ๋ฆฌ๋ฅผ ๋ง๋ญ๋๋ค.
- Developer Console์์ File(ํ์ผ) | Open Resource(๋ฆฌ์์ค ์ด๊ธฐ)๋ฅผ ํด๋ฆญํฉ๋๋ค.
- Account.obj๋ฅผ ์ ํํ ํ Open(์ด๊ธฐ)์ ํด๋ฆญํฉ๋๋ค.
- Id์ Name(์ด๋ฆ)์ ์ ํํฉ๋๋ค. ์ฌ๋ฌ ํ๋๋ฅผ ์ ํํ๋ ค๋ฉด CTRL(Windows) ๋๋ CMD(Mac)๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- Query(์ฟผ๋ฆฌ)๋ฅผ ํด๋ฆญํฉ๋๋ค.
- ์ฟผ๋ฆฌ ํธ์ง๊ธฐ์ ์ฟผ๋ฆฌ ์์ฑ ์ง์นจ์ด ํ์๋๋ฉด Query(์ฟผ๋ฆฌ)๋ฅผ ๋ค์ ํด๋ฆญํฉ๋๋ค.
- ์ฟผ๋ฆฌ ๋์
WHERE Name = 'Recruiting'์ ์ ๋ ฅํฉ๋๋ค. - SOQL ์ฟผ๋ฆฌ๊ฐ ๋ค์๊ณผ ๊ฐ์ด ํ์๋๋์ง ํ์ธํฉ๋๋ค.
SELECT Id, Name FROM Account WHERE Name = 'Recruiting' - Execute(์คํ)๋ฅผ ํด๋ฆญํ์ฌ ์ฟผ๋ฆฌ๋ฅผ ์คํํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ํ์ํฉ๋๋ค.
์๋ก ๋ง๋ Contact(์ฐ๋ฝ์ฒ)๋ฅผ Recruiting Account(์ฑ์ฉ ๊ณ์ข)์ ์ฐ๊ฒฐํ๊ณ SOQL ์ฟผ๋ฆฌ๋ฅผ createContact ๋ฉ์๋์ ์ถ๊ฐํฉ๋๋ค.
- CreateContactFromCan ํด๋์ค์์ ๊ธฐ์กด ์ฝ๋๋ฅผ ๋ค์ ์ฝ๋๋ก ๋ฐ๊ฟ๋๋ค.
public with sharing class CreateContactFromCan { //Declare a method that returns void and accepts a Candidate list named candsFromTrigger public static void createContact (List<Candidate__c> candsFromTrigger) { //Select the Recruiting account from the database and add it to a list //named candAccts (from the Account class) List<Account> candAccts = [SELECT Id, Name FROM Account WHERE Name = 'Recruiting']; //Instantiate a Contact list named conList List<Contact> conList = new List<Contact>(); //Declare a FOR list loop that uses an iteration variable named currentCandidate //to loop through the candsFromTrigger list for(Candidate__c currentCandidate:candsFromTrigger) { //Create a Contact and add it to conList //Set the FirstName, LastName, and Email attributes to match the //corresponding attributes of the currentCandidate object (First_Name__c, //Last_Name__c, and Email__c) conList.add(new Contact( AccountId = candAccts[0].Id, FirstName = currentCandidate.First_Name__c, LastName = currentCandidate.Last_Name__c, Email = currentCandidate.Email__c) ); } //Save conList to the database Database.insert(conList); } } - CreateContactFromCan ํด๋์ค๋ฅผ ์ ์ฅํฉ๋๋ค.
