LoginSignup
3
4

More than 5 years have passed since last update.

【Salesforce初心者】SOSLで複数のSObjectからの結果を取り出す書き方

Posted at

SOSLで複数のオブジェクトに検索をかけた結果を取り出す時に使える書き方をTrailheadから学んだ。
Trailheadの例文だが、コメントを入れただけ。メモメモ。

List<List<SObject>> searchlist = //WingoとSFDCの検索結果リストを格納するリストを宣言
    [FIND 'Wingo OR SFDC' IN ALL FIELDS 
    RETURNING Account (Name), Contact(FirstName, LastName, Department)];

//searchlistの中の1個目のリスト(Accountで返されたリスト)をAccount[]でキャスト
Account[] searchAccounts = (Account[])searchlist[0];


//searchlistの中の2個目のリスト(Contactで返されたリスト)をContact[]でキャスト
Contact[] searchContacts = (Contact[])searchlist[1];

System.debug('Found the following accounts.');
for (Account a : searchAccounts) {
    System.debug(a.Name);
}

System.debug('Found the following contacts.');
    for (Contact c : searchContacts){
        System.debug(c.LastName + ' '+ c.FirstName);
    }

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4