La versione 2+ del driver Salesforce ODBC consente di eseguire in batch più istruzioni SOQL Insert. Questo blog mostra come inserire più record di Microsoft Access in Salesforce.
Per iniziare:
- Installare e concedere in licenza il driver ODBC Salesforce.com sulla macchina in cui è installato Microsoft Access.
Prima di poter utilizzare Salesforce.com ODBC Driver per connettere l'applicazione a Salesforce.com, è necessario configurare un'origine dati ODBC. Un'origine dati ODBC memorizza i dettagli di connessione per il database di destinazione (ad es. Salesforce.com) e il driver ODBC necessario per connettersi ad esso (ad es. il driver ODBC di Salesforce.com).
Per eseguire l'amministratore ODBC (che usi per creare un'origine dati), nella finestra di dialogo Esegui di Windows, digita questo comando se stai utilizzando una versione a 64 bit di Microsoft Office:
%windir%\system32\odbcad32.exe
–Oppure–
Digita questo comando se stai utilizzando una versione a 32 bit di Microsoft Office:
%windir%\syswow64\odbcad32.exe
Se non sei sicuro che la tua versione di Microsoft Office sia a 32 o 64 bit, avvia un'applicazione Office, ad es. Microsoft Access e quindi cercare il processo dell'applicazione in Task Manager. Se il nome del processo è (per Microsoft Access) MSACCESS.EXE *32, Microsoft Office è a 32 bit. Se il nome del processo è MSACCESS.EXE, Microsoft Office è a 64 bit.
Per creare un'origine dati del driver ODBC Salesforce.com:
- In Amministratore ODBC, scegli la scheda DSN di sistema, quindi scegli Aggiungi.
- Nella finestra di dialogo Crea nuova origine dati, scegli Easysoft Salesforce ODBC SOQL Driver, quindi scegli Fine.
- Completare la finestra di dialogo Configurazione DSN del driver ODBC di Easysoft Salesforce SOQL:
Impostazioni Valore DSN SFSOQL Nome utente Il nome del tuo utente Salesforce.com. Ad esempio, mioutente@miodominio.com. Password La password per l'utente Salesforce.com. Segnale Il token di sicurezza per l'utente Salesforce.com, se richiesto. Per scoprire se è necessario fornire un token di sicurezza, scegli il pulsante Test. Se il tentativo di connessione fallisce con un errore che contiene
LOGIN_MUST_USE_SECURITY_TOKEN, devi fornirne uno.Salesforce.com invia tramite e-mail il token di sicurezza all'indirizzo e-mail associato al tuo account utente Salesforce.com. Se non hai ricevuto un token di sicurezza, puoi rigenerarlo. Salesforce.com ti invierà quindi un'e-mail con il nuovo token di sicurezza. Per rigenerare il token di sicurezza, accedi a Salesforce.com e quindi scegli Configurazione dal menu utente. Cerca "token di sicurezza" nella casella Ricerca veloce. Fare clic su Reimposta token di sicurezza nella pagina Reimposta token di sicurezza. Quando ricevi il token nel tuo client di posta, copialo e incollalo nel campo Token.
- Utilizza il pulsante Test per verificare di poterti connettere correttamente a Salesforce.com.
Accesso Microsoft
- Crea un nuovo database Microsoft Access.
- Crea una tabella chiamata Account con queste colonne:
Colonna Tipo di dati ID Numero automatico NomeAcc Testo breve Descrizione della proprietà Testo breve Indirizzo Testo breve Città Testo breve Codice postale Testo breve - Inserisci alcuni dati di esempio nella tabella. Ad esempio:
AccName Property Description Address Town PostCode MyCo Head Office 1 MyStreet MyTown AB1 DEF AcmeLtd Workshop 1 MyRoad MyTown AB1 XYZ
- Premi ALT+F11 per avviare Visual Basic Editor.
- Inserisci un nuovo modulo e aggiungi il codice seguente. Ci sono due subroutine e una funzione di supporto. Entrambe le subroutine inseriscono i record di Access in Salesforce in blocco. La seconda subroutine mostra come utilizzare un'istruzione di inserimento SOQL parametrizzata.
- Nel menu Esegui, usa Esegui sub/modulo utente per eseguire le subroutine.
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