Ab Version 2 des Salesforce-ODBC-Treibers können Sie mehrere SOQL-Insert-Anweisungen bündeln. Dieser Blog zeigt Ihnen, wie Sie mehrere Microsoft Access-Datensätze in Salesforce einfügen.
Um loszulegen:
- Installieren und lizenzieren Sie den Salesforce.com-ODBC-Treiber auf dem Computer, auf dem Microsoft Access installiert ist.
Bevor Sie den Salesforce.com-ODBC-Treiber verwenden können, um Ihre Anwendung mit Salesforce.com zu verbinden, müssen Sie eine ODBC-Datenquelle konfigurieren. Eine ODBC-Datenquelle speichert die Verbindungsdetails für die Zieldatenbank (z. B. Salesforce.com) und den ODBC-Treiber, der für die Verbindung dazu erforderlich ist (z. B. der ODBC-Treiber von Salesforce.com).
Um den ODBC-Administrator (den Sie zum Erstellen einer Datenquelle verwenden) auszuführen, geben Sie im Windows-Dialogfeld „Ausführen“ diesen Befehl ein, wenn Sie eine 64-Bit-Version von Microsoft Office verwenden:
%windir%\system32\odbcad32.exe
–Oder–
Geben Sie diesen Befehl ein, wenn Sie eine 32-Bit-Version von Microsoft Office verwenden:
%windir%\syswow64\odbcad32.exe
Wenn Sie sich nicht sicher sind, ob Ihre Version von Microsoft Office 32-Bit oder 64-Bit ist, starten Sie eine Office-Anwendung, z. Microsoft Access, und suchen Sie dann im Task-Manager nach dem Prozess der Anwendung. Wenn der Prozessname (für Microsoft Access) MSACCESS.EXE *32 lautet, ist Microsoft Office 32-Bit. Wenn der Prozessname MSACCESS.EXE ist, ist Microsoft Office 64-Bit.
So erstellen Sie eine Salesforce.com-ODBC-Treiberdatenquelle:
- Wählen Sie im ODBC-Administrator die Registerkarte System-DSN und dann Hinzufügen.
- Wählen Sie im Dialogfeld „Neue Datenquelle erstellen“ die Option „Easysoft Salesforce ODBC SOQL-Treiber“ und dann „Fertig stellen“.
- Vervollständigen Sie das Dialogfeld Easysoft Salesforce SOQL ODBC Driver DSN Setup:
Setting Wert DSN SFSOQL Benutzername Der Name Ihres Salesforce.com-Benutzers. Beispiel:meinbenutzer@meinedomain.com. Passwort Das Passwort für Ihren Salesforce.com-Benutzer. Token Das Sicherheitstoken für Ihren Salesforce.com-Benutzer, falls erforderlich. Um herauszufinden, ob Sie ein Sicherheitstoken angeben müssen, wählen Sie die Schaltfläche Test. Wenn der Verbindungsversuch mit einem Fehler fehlschlägt, der
LOGIN_MUST_USE_SECURITY_TOKENenthält , müssen Sie einen angeben.Salesforce.com sendet das Sicherheitstoken per E-Mail an die E-Mail-Adresse, die Ihrem Salesforce.com-Benutzerkonto zugeordnet ist. Wenn Sie kein Sicherheitstoken erhalten haben, können Sie es neu generieren. Salesforce.com sendet Ihnen dann das neue Sicherheitstoken per E-Mail zu. Melden Sie sich zum erneuten Generieren Ihres Sicherheitstokens bei Salesforce.com an und wählen Sie dann Setup aus dem Benutzermenü. Suchen Sie im Feld „Schnellsuche“ nach „Sicherheitstoken“. Klicken Sie auf der Seite „Sicherheitstoken zurücksetzen“ auf „Sicherheitstoken zurücksetzen“. Wenn Sie das Token in Ihrem E-Mail-Client erhalten, kopieren Sie es und fügen Sie es dann in das Token-Feld ein.
- Verwenden Sie die Test-Schaltfläche, um zu überprüfen, ob Sie erfolgreich eine Verbindung zu Salesforce.com herstellen können.
Microsoft Access
- Erstellen Sie eine neue Microsoft Access-Datenbank.
- Erstellen Sie eine Tabelle namens Account mit diesen Spalten:
Column Datentyp ID AutoNummer Zugriffsname Kurztext Eigenschaftsbeschreibung Kurztext Adresse Kurztext Stadt Kurztext Postleitzahl Kurztext - Geben Sie einige Beispieldaten in die Tabelle ein. Beispiel:
AccName Property Description Address Town PostCode MyCo Head Office 1 MyStreet MyTown AB1 DEF AcmeLtd Workshop 1 MyRoad MyTown AB1 XYZ
- Drücken Sie ALT+F11, um den Visual Basic-Editor zu starten.
- Fügen Sie ein neues Modul ein und fügen Sie den folgenden Code hinzu. Es gibt zwei Subroutinen und eine Hilfsfunktion. Beide Subroutinen fügen die Access-Datensätze in großen Mengen in Salesforce ein. Die zweite Unterroutine zeigt, wie eine parametrisierte SOQL-Einfügungsanweisung verwendet wird.
- Im Menü „Ausführen“ verwenden Sie „Run Sub/UserForm“, um die Subroutinen auszuführen.
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