search コール例
次の Java および C# の例では、Partner WSDL の search() コールの使用方法を示します。各例では、SOQL クエリで使用されている電話番号の文字列を受け入れます。search コールでは、すべての取引先責任者、リード、および取引先の電話番号項目の中から、渡された値に一致する項目を検索します。次に、この例では、一致するレコードを含む返された検索結果を反復処理し、配列にそれらを追加し、コンソールにその項目の値を書き込みます。返されたレコード項目は、各レコードタイプの SOQL クエリで指定された項目に対応します。
サンプルメソッドを実行するには、Partner WSDL の使用例で提供されている対応する Java または C# テンプレートクラスを使用できます。
Java の例
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public void searchSample(String phoneNumber) {
18 try {
19 // Example of phoneNumber format: 4155551212
20 String soslQuery =
21 "FIND {" + phoneNumber + "} IN Phone FIELDS " +
22 "RETURNING " +
23 "Contact(Id, Phone, FirstName, LastName), " +
24 "Lead(Id, Phone, FirstName, LastName)," +
25 "Account(Id, Phone, Name)";
26 // Perform SOSL query
27 SearchResult sResult = partnerConnection.search(soslQuery);
28 // Get the records returned by the search result
29 SearchRecord[] records = sResult.getSearchRecords();
30 // Create lists of objects to hold search result records
31 List<SObject> contacts = new ArrayList<SObject>();
32 List<SObject> leads = new ArrayList<SObject>();
33 List<SObject> accounts = new ArrayList<SObject>();
34
35 // Iterate through the search result records
36 // and store the records in their corresponding lists
37 // based on record type.
38 if (records != null && records.length > 0) {
39 for (int i = 0; i < records.length; i++){
40 SObject record = records[i].getRecord();
41 if (record.getType().toLowerCase().equals("contact")) {
42 contacts.add(record);
43 } else if (record.getType().toLowerCase().equals("lead")){
44 leads.add(record);
45 } else if (record.getType().toLowerCase().equals("account")) {
46 accounts.add(record);
47 }
48 }
49 // Display the contacts that the search returned
50 if (contacts.size() > 0) {
51 System.out.println("Found " + contacts.size() +
52 " contact(s):");
53 for (SObject contact : contacts) {
54 System.out.println(contact.getId() + " - " +
55 contact.getField("FirstName") + " " +
56 contact.getField("LastName") + " - " +
57 contact.getField("Phone")
58 );
59 }
60 }
61 // Display the leads that the search returned
62 if (leads.size() > 0) {
63 System.out.println("Found " + leads.size() +
64 " lead(s):");
65 for (SObject lead : leads) {
66 System.out.println(lead.getId() + " - " +
67 lead.getField("FirstName") + " " +
68 lead.getField("LastName") + " - " +
69 lead.getField("Phone")
70 );
71 }
72 }
73 // Display the accounts that the search returned
74 if (accounts.size() > 0) {
75 System.out.println("Found " +
76 accounts.size() + " account(s):");
77 for (SObject account : accounts) {
78 System.out.println(account.getId() + " - " +
79 account.getField("Name") + " - " +
80 account.getField("Phone")
81 );
82 }
83 }
84 } else {
85 // The search returned no records
86 System.out.println("No records were found for the search.");
87 }
88 } catch (ConnectionException ce) {
89 ce.printStackTrace();
90 }
91}C# の例
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public void searchSample(String phoneNumber)
18{
19 try
20 {
21 // Example of phoneNumber format: 4155551212
22 String soslQuery =
23 "FIND {" + phoneNumber + "} IN Phone FIELDS " +
24 "RETURNING " +
25 "Contact(Id, Phone, FirstName, LastName), " +
26 "Lead(Id, Phone, FirstName, LastName)," +
27 "Account(Id, Phone, Name)";
28 // Perform SOSL query
29 SearchResult sResult = binding.search(soslQuery);
30 // Get the records returned by the search result
31 SearchRecord[] records = sResult.searchRecords;
32 // Create lists of objects to hold search result records
33 ArrayList contacts = new System.Collections.ArrayList();
34 ArrayList leads = new System.Collections.ArrayList();
35 ArrayList accounts = new System.Collections.ArrayList();
36
37 // Iterate through the search result records
38 // and store the records in their corresponding lists
39 // based on record type.
40 if ((records != null) && (records.Length > 0))
41 {
42 for (int i = 0; i < records.Length; i++)
43 {
44 sObject record = records[i].record;
45
46 if (record.type.ToLower().Equals("contact"))
47 {
48 contacts.Add(record);
49 }
50 else if (record.type.ToLower().Equals("lead"))
51 {
52 leads.Add(record);
53 }
54 else if (record.type.ToLower().Equals("account"))
55 {
56 accounts.Add(record);
57 }
58 }
59 // Display the contacts that the search returned
60 if (contacts.Count > 0)
61 {
62 Console.WriteLine("Found " + contacts.Count + " contact(s):");
63 for (int i = 0; i < contacts.Count; i++)
64 {
65 sObject c = (sObject)contacts[i];
66 Console.WriteLine(c.Any[0].InnerText + " - " +
67 c.Any[2].InnerText + " " +
68 c.Any[3].InnerText + " - " + c.Any[1].InnerText);
69 }
70 }
71 // Display the leads that the search returned
72 if (leads.Count > 0)
73 {
74 Console.WriteLine("Found " + leads.Count + " lead(s):");
75 for (int i = 0; i < leads.Count; i++)
76 {
77 sObject l = (sObject)leads[i];
78 Console.WriteLine(l.Any[0].InnerText + " - " +
79 l.Any[2].InnerText + " " +
80 l.Any[3].InnerText + " - " + l.Any[1].InnerText);
81 }
82 }
83 // Display the accounts that the search returned
84 if (accounts.Count > 0)
85 {
86 Console.WriteLine("Found " + accounts.Count + " account(s):");
87 for (int i = 0; i < accounts.Count; i++)
88 {
89 sObject a = (sObject)accounts[i];
90 Console.WriteLine(a.Any[0].InnerText + " - " +
91 a.Any[2].InnerText + " - " +
92 a.Any[1].InnerText);
93 }
94 }
95 }
96 else
97 {
98 // The search returned no records
99 Console.WriteLine("No records were found for the search.");
100 }
101 }
102 catch (SoapException e)
103 {
104 Console.WriteLine("An unexpected error has occurred: " + e.Message +
105 " Stack trace: " + e.StackTrace);
106 }
107}