この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

Connection クラス

Salesforce 組織で外部システムのスキーマと同期し、外部データのクエリ、検索、および書き込み操作 (更新/挿入と削除) を処理できるようにするには、このクラスを拡張します。このクラスは、DataSourceUtil クラスを拡張し、そのメソッドを継承します。

名前空間

DataSource

使用方法

DataSource.Connection および DataSource.Provider クラスによって、Salesforce Connect のカスタムアダプタが構成されます。

DataSource.Connection クラスの sync メソッドを変更しても、外部オブジェクトが自動的に再同期されることはありません。

1global class SampleDataSourceConnection extends DataSource.Connection {
2    global SampleDataSourceConnection(DataSource.ConnectionParams connectionParams) {
3    }
4    
5    override global List<DataSource.Table> sync() {
6        List<DataSource.Table> tables = new List<DataSource.Table>();        
7        List<DataSource.Column> columns;
8        columns = new List<DataSource.Column>();
9        columns.add(DataSource.Column.text('Name', 255));
10        columns.add(DataSource.Column.text('ExternalId', 255));
11        columns.add(DataSource.Column.url('DisplayUrl'));
12        tables.add(DataSource.Table.get('Sample', 'Title', columns));
13        return tables;
14    }
15    
16    override global DataSource.TableResult query(DataSource.QueryContext c) {
17        return DataSource.TableResult.get(c, DataSource.QueryUtils.process(c, getRows()));
18    }
19    
20    override global List<DataSource.TableResult> search(DataSource.SearchContext c) {        
21        List<DataSource.TableResult> results = new List<DataSource.TableResult>();
22        for (DataSource.TableSelection tableSelection : c.tableSelections) {
23            results.add(DataSource.TableResult.get(tableSelection, getRows()));
24        }
25        return results;
26    }
27
28    // Helper method to get record values from the external system for the Sample table.
29    private List<Map<String, Object>> getRows () {
30        // Get row field values for the Sample table from the external system via a callout.
31        HttpResponse response = makeGetCallout();
32        // Parse the JSON response and populate the rows.
33        Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(
34                response.getBody());
35        Map<String, Object> error = (Map<String, Object>)m.get('error');
36        if (error != null) {
37            throwException(string.valueOf(error.get('message')));
38        }
39        List<Map<String,Object>> rows = new List<Map<String,Object>>();
40        List<Object> jsonRows = (List<Object>)m.get('value');
41        if (jsonRows == null) {
42            rows.add(foundRow(m));
43        } else {
44            for (Object jsonRow : jsonRows) {
45                Map<String,Object> row = (Map<String,Object>)jsonRow;
46                rows.add(foundRow(row));
47            }
48        }
49        return rows;
50    }
51    
52    global override List<DataSource.UpsertResult> upsertRows(DataSource.UpsertContext
53            context) {
54       if (context.tableSelected == 'Sample') {
55           List<DataSource.UpsertResult> results = new List<DataSource.UpsertResult>();
56           List<Map<String, Object>> rows = context.rows;
57           
58           for (Map<String, Object> row : rows){
59              // Make a callout to insert or update records in the external system.
60              HttpResponse response;
61              // Determine whether to insert or update a record.
62              if (row.get('ExternalId') == null){
63                 // Send a POST HTTP request to insert new external record.
64                 // Make an Apex callout and get HttpResponse.
65                 response = makePostCallout(
66                     '{"name":"' + row.get('Name') + '","ExternalId":"' + 
67                     row.get('ExternalId') + '"');
68              }
69              else {
70                 // Send a PUT HTTP request to update an existing external record.
71                 // Make an Apex callout and get HttpResponse.
72                 response = makePutCallout(
73                     '{"name":"' + row.get('Name') + '","ExternalId":"' + 
74                     row.get('ExternalId') + '"',
75                     String.valueOf(row.get('ExternalId')));
76              }
77         
78              // Check the returned response.
79              // First, deserialize it.
80              Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(
81                      response.getBody());
82              if (response.getStatusCode() == 200){
83                  results.add(DataSource.UpsertResult.success(
84                          String.valueOf(m.get('id'))));
85              } 
86              else {
87                 results.add(DataSource.UpsertResult.failure(
88                                 String.valueOf(m.get('id')), 
89                     'The callout resulted in an error: ' + 
90                     response.getStatusCode()));
91              }
92           } 
93           return results;
94       } 
95       return null;
96    }
97         
98    global override List<DataSource.DeleteResult> deleteRows(DataSource.DeleteContext 
99            context) {
100       if (context.tableSelected == 'Sample'){
101           List<DataSource.DeleteResult> results = new List<DataSource.DeleteResult>();
102           for (String externalId : context.externalIds){
103              HttpResponse response = makeDeleteCallout(externalId);
104              if (response.getStatusCode() == 200){
105                 results.add(DataSource.DeleteResult.success(externalId));
106              } 
107              else {
108                 results.add(DataSource.DeleteResult.failure(externalId, 
109                             'Callout delete error:' 
110                             + response.getBody()));
111              }
112           }
113           return results;
114       }
115       return null;
116     }
117     
118    // Helper methods
119        
120    // Make a GET callout
121     private static HttpResponse makeGetCallout() {
122         HttpResponse response;
123         // Make callout
124         // ...
125         return response;
126     }
127     
128     // Populate a row based on values from the external system.
129     private Map<String,Object> foundRow(Map<String,Object> foundRow) {
130        Map<String,Object> row = new Map<String,Object>();
131        row.put('ExternalId', string.valueOf(foundRow.get('Id')));
132        row.put('DisplayUrl', string.valueOf(foundRow.get('DisplayUrl')));
133        row.put('Name', string.valueOf(foundRow.get('Name')));        
134        return row;
135    }
136
137     // Make a POST callout
138     private static HttpResponse makePostCallout(String jsonBody) {
139         HttpResponse response;
140         // Make callout
141         // ...
142         return response;
143     }
144     
145     // Make a PUT callout
146     private static HttpResponse makePutCallout(String jsonBody, String externalID) {
147         HttpResponse response;
148         // Make callout
149         // ...
150         return response;
151     }
152     
153     // Make a DELETE callout
154     private static HttpResponse makeDeleteCallout(String externalID) {
155         HttpResponse response;
156         // Make callout
157         // ...
158         return response;
159     }
160}

Connection のメソッド

Connection のメソッドは次のとおりです。

deleteRows(deleteContext)

Salesforce ユーザインターフェース、API、または Apex 経由で外部オブジェクトレコードが削除されたときに呼び出されます。

署名

public List<DataSource.DeleteResult> deleteRows(DataSource.DeleteContext deleteContext)

パラメータ

deleteContext
型: DataSource.DeleteContext
削除要求に関するコンテキスト情報が含まれます。

戻り値

型: List<DataSource.DeleteResult>

削除操作の結果。

query(queryContext)

外部オブジェクトの SOQL クエリによって呼び出されます。SOQL クエリは、Salesforce でユーザが外部オブジェクトのリストビューまたはレコード詳細ページにアクセスしたときに生成および実行されます。クエリの結果を返します。

署名

public DataSource.TableResult query(DataSource.QueryContext queryContext)

パラメータ

queryContext
型: DataSource.QueryContext
データテーブルに対して実行するクエリを表します。

戻り値

型: DataSource.TableResult

sync()

システム管理者が外部データソースの詳細ページで [検証して同期] をクリックしたときに呼び出されます。外部システムのスキーマを示すテーブルのリストを返します。

署名

public List<DataSource.Table> sync()

戻り値

型: List<DataSource.Table>

返された各テーブルは、Salesforce での外部オブジェクトの作成に使用できます。システム管理者は [外部データソースの検証] ページで返されたテーブルのリストを参照し、同期するテーブルを選択します。システム管理者が [同期] をクリックしたときに、選択された各テーブルに対して外部オブジェクトが作成されます。また、選択されたテーブル内の各列は外部オブジェクトの項目になります。

upsertRows(upsertContext)

Salesforce ユーザインターフェース、API、または Apex 経由で外部オブジェクトレコードが作成または更新されたときに呼び出されます。

署名

public List<DataSource.UpsertResult> upsertRows(DataSource.UpsertContext upsertContext)

パラメータ

upsertContext
型: DataSource.UpsertContext
更新/挿入要求に関するコンテキスト情報が含まれます。

戻り値

型: List<DataSource.UpsertResult>

更新/挿入操作の結果。