Salesforce ODBCドライバーのバージョン2+を使用すると、複数のSOQL挿入ステートメントをバッチ処理できます。このブログでは、複数のMicrosoftAccessレコードをSalesforceに挿入する方法を紹介しています。
開始するには:
- MicrosoftAccessがインストールされているマシンにSalesforce.comODBCドライバーをインストールしてライセンスを取得します。
Salesforce.com ODBCドライバを使用してアプリケーションをSalesforce.comに接続する前に、ODBCデータソースを設定する必要があります。 ODBCデータソースには、ターゲットデータベース(Salesforce.comなど)とそれに接続するために必要なODBCドライバー(Salesforce.com ODBCドライバーなど)の接続の詳細が格納されます。
ODBCアドミニストレータ(データソースの作成に使用)を実行するには、64ビットバージョンのMicrosoft Officeを使用している場合は、[Windowsの実行]ダイアログボックスで次のコマンドを入力します。
%windir%\system32\odbcad32.exe
–または–
32ビットバージョンのMicrosoftOfficeを使用している場合は、次のコマンドを入力します。
%windir%\syswow64\odbcad32.exe
お使いのMicrosoftOfficeのバージョンが32ビットか64ビットかわからない場合は、Officeアプリケーションを起動してください。 Microsoft Accessをクリックし、タスクマネージャーでアプリケーションのプロセスを探します。プロセス名が(Microsoft Accessの場合)MSACCESS.EXE * 32の場合、MicrosoftOfficeは32ビットです。プロセス名がMSACCESS.EXEの場合、MicrosoftOfficeは64ビットです。
Salesforce.com ODBCドライバーデータソースを作成するには:
- ODBC Administratorで、[システムDSN]タブを選択し、[追加]を選択します。
- [新しいデータソースの作成]ダイアログボックスで、[Easysoft Salesforce ODBC SOQLドライバー]を選択し、[完了]を選択します。
- Easysoft Salesforce SOQL ODBCドライバーDSNセットアップダイアログボックスを完了します:
設定 値 DSN SFSOQL ユーザー名 Salesforce.comユーザーの名前。例:[email protected]。 パスワード Salesforce.comユーザーのパスワード。 トークン 必要に応じて、Salesforce.comユーザーのセキュリティトークン。 セキュリティトークンを提供する必要があるかどうかを確認するには、[テスト]ボタンを選択します。
LOGIN_MUST_USE_SECURITY_TOKEN
を含むエラーで接続の試行が失敗した場合 、1つ提供する必要があります。Salesforce.comは、Salesforce.comユーザーアカウントに関連付けられている電子メールアドレスにセキュリティトークンを電子メールで送信します。セキュリティトークンを受け取っていない場合は、再生成できます。その後、Salesforce.comから新しいセキュリティトークンがメールで送信されます。セキュリティトークンを再生成するには、Salesforce.comにログインし、ユーザーメニューから[設定]を選択します。 [クイック検索]ボックスで「セキュリティトークン」を検索します。 [セキュリティトークンのリセット]ページで[セキュリティトークンのリセット]をクリックします。メールクライアントでトークンを受け取ったら、それをコピーして、[トークン]フィールドに貼り付けます。
- [テスト]ボタンを使用して、Salesforce.comに正常に接続できることを確認します。
Microsoft Access
- 新しいMicrosoftAccessデータベースを作成します。
- 次の列を使用してAccountというテーブルを作成します:
Column データ型 ID AutoNumber AccName ShortText プロパティの説明 ShortText 住所 ShortText 町 ShortText PostCode ShortText - サンプルデータをテーブルに入力します。例:
AccName Property Description Address Town PostCode MyCo Head Office 1 MyStreet MyTown AB1 DEF AcmeLtd Workshop 1 MyRoad MyTown AB1 XYZ
- Alt + F11を押して、VisualBasicEditorを起動します。
- 新しいモジュールを挿入し、次のコードを追加します。 2つのサブルーチンとヘルパー関数があります。どちらのサブルーチンも、AccessレコードをSalesforceに一括で挿入します。 2番目のサブルーチンは、パラメーター化されたSOQL挿入ステートメントの使用方法を示しています。
- [実行]メニューで、[Sub/UserFormの実行]を使用してサブルーチンを実行します。
Option Compare Database Sub InsertAccounts() Dim con As New ADODB.Connection Dim comm As New ADODB.Command Dim PrmName As New ADODB.Parameter Dim PrmAddress As New ADODB.Parameter Dim PrmTown As New ADODB.Parameter Dim PrmPostCode As New ADODB.Parameter Dim PrmDescription As New ADODB.Parameter Dim RowCount As Long Dim i As Integer Dim db As DAO.Database Dim rs As DAO.Recordset Dim BlockCount As String Dim isPosted As Boolean RowCount = 0 ' Open the connection to the Easysoft Salesforce SOQL ODBC Driver data source con.Open "SFSOQL" comm.ActiveConnection = con ' Set up the initial insert statement using ? for each column I am going to pass in comm.CommandText = "insert into Account (Name, BillingStreet, BillingCity, BillingPostalCode, Description ) values ( ?, ?, ?, ?, ? )" ' Bind all the columns to the statement Set PrmName = comm.CreateParameter("P1", adVarWChar, adParamInput, 255, Null) Set PrmAddress = comm.CreateParameter("P2", adVarWChar, adParamInput, 255, Null) Set PrmTown = comm.CreateParameter("P3", adVarWChar, adParamInput, 120, Null) Set PrmPostCode = comm.CreateParameter("P4", adVarWChar, adParamInput, 60, Null) Set PrmDescription = comm.CreateParameter("P5", adLongVarWChar, adParamInput, 255, Null) comm.Parameters.Append PrmName comm.Parameters.Append PrmAddress comm.Parameters.Append PrmTown comm.Parameters.Append PrmPostCode comm.Parameters.Append PrmDescription ' Create a connection to the local database and start working through the rows Set db = CurrentDb Set rs = db.OpenRecordset("select * from Account order by Id") BlockCount = 0 Do While Not rs.EOF RowCount = RowCount + 1 If BlockCount = 0 Then ' Start a new transaction con.BeginTrans isPosted = False End If BlockCount = BlockCount + 1 Debug.Print RowCount & " : " & rs.Fields("AccName") DoEvents ' Prepare to transfer the data to the ODBC driver PrmName.Value = rs.Fields("AccName") If Not IsNull(rs.Fields("Address")) Then PrmAddress.Value = Replace(rs.Fields("Address"), ",", vbCrLf) Else PrmAddress.Value = Null End If If Not IsNull(rs.Fields("Town")) Then PrmTown.Value = rs.Fields("Town") Else PrmTown.Value = Null End If If Not IsNull(rs.Fields("Town")) Then PrmPostCode.Value = rs.Fields("PostCode") Else PrmPostCode.Value = Null End If If Not IsNull(rs.Fields("Property Description")) Then PrmDescription.Value = rs.Fields("Property Description") Else PrmDescription.Value = Null End If comm.Execute ' When 200 rows have been sent to the driver, commit If BlockCount = 200 Then Debug.Print "Block posted" con.CommitTrans isPosted = True BlockCount = 0 End If ' Loop through the block until the end is reached rs.MoveNext Loop rs.Close db.Close ' Finally, if there are any rows left to commit, send them If Not isPosted Then con.CommitTrans con.Close End Sub Sub InsertAccountsParameterisedSOQL() Dim con As New ADODB.Connection Dim SQL As String Dim SQLBase As String Dim BlockCount As Long Dim isPosted As Boolean Dim RowCount As Long Dim i As Integer Dim db As DAO.Database Dim rs As DAO.Recordset RowCount = 0 ' Open the connection to the Easysoft Salesforce SOQL ODBC Driver data source con.Open "SFSOQL" SQLBase = "insert into Account (Name, BillingStreet, BillingCity, BillingPostalCode, Description ) values ( " ' Create a connection to the local database and start working through the rows Set db = CurrentDb Set rs = db.OpenRecordset("select * from Account order by Id") BlockCount = 0 Do While Not rs.EOF RowCount = RowCount + 1 If BlockCount = 0 Then ' Start a new transaction con.BeginTrans isPosted = False End If BlockCount = BlockCount + 1 Debug.Print RowCount & " : " & rs.Fields("AccName") DoEvents ' Prepare to transfer the data to the ODBC driver SQL = SQLBase If IsNull(rs.Fields("AccName")) Then SQL = SQL & "NULL, " Else SQL = SQL & "'" & EscQuotes(rs.Fields("AccName")) & "', " End If If IsNull(rs.Fields("Address")) Then SQL = SQL & "NULL, " Else SQL = SQL & "'" & EscQuotes(Replace(rs.Fields("Address"), ",", vbCrLf)) & "', " End If If Not IsNull(rs.Fields("Town")) Then SQL = SQL & "NULL, " Else SQL = SQL & "'" & EscQuotes(rs.Fields("Town")) & "', " End If If IsNull(rs.Fields("PostCode")) Then SQL = SQL & "NULL, " Else SQL = SQL & "'" & EscQuotes(rs.Fields("PostCode")) & "', " End If If IsNull(rs.Fields("Property Description")) Then SQL = SQL & "NULL) " Else SQL = SQL & "'" & EscQuotes(rs.Fields("Property Description")) & "')" End If con.Execute SQL ' When 200 rows have been sent to the driver then commit If BlockCount = 200 Then Debug.Print "Block posted" con.CommitTrans isPosted = True BlockCount = 0 End If ' Loop through the block until the end is reached rs.MoveNext Loop rs.Close db.Close ' Finally, if there are any rows left to commit, send them If Not isPosted Then con.CommitTrans con.Close End Sub Function EscQuotes(inpStr As String) As String EscQuotes = Replace(inpStr, "'", "''") End Function