SQL >> Base de Datos >  >> RDS >> Access

Carga masiva de registros de Microsoft Access en Salesforce

La versión 2 y posteriores del controlador ODBC para Salesforce pueden agrupar varias inserciones SOQL, lo que hace viable volcar una tabla completa de Access en Salesforce por bloques en lugar de fila a fila. Instala y licencia el controlador en la máquina que ejecuta Access.

Crear el origen de datos

Abre el Administrador de ODBC desde el cuadro Ejecutar, de acuerdo con la arquitectura de tu Office: %windir%\system32\odbcad32.exe para Office de 64 bits y %windir%\syswow64\odbcad32.exe para 32 bits. Para saber cuál tienes, arranca Access y mira el nombre del proceso en el Administrador de tareas: MSACCESS.EXE *32 es 32 bits y MSACCESS.EXE es 64 bits.

  1. Abre la pestaña System DSN y pulsa Add.
  2. Elige Easysoft Salesforce ODBC SOQL Driver y pulsa Finish.
  3. Completa el cuadro de configuración:
    SettingValue
    DSNSFSOQL
    User NameTu usuario de Salesforce, p. ej. myuser@mydomain.com
    PasswordLa contraseña de ese usuario
    TokenTu security token, si es necesario. Pulsa Test para averiguarlo: un error que contenga LOGIN_MUST_USE_SECURITY_TOKEN indica que hace falta uno. Salesforce envía el token por correo a la dirección de tu cuenta; si no llegó, entra, elige Setup en el menú de usuario, busca "security token" en Quick Find y usa Reset Security Token. Pega aquí el valor recibido.
  4. Pulsa Test para confirmar la conexión.

Preparar el lado de Access

  1. Crea una base de datos Access nueva con una tabla llamada Account:
    ColumnData Type
    IDAutoNumber
    AccNameShortText
    Property DescriptionShortText
    AddressShortText
    TownShortText
    PostCodeShortText
  2. Añade unas filas de ejemplo:
    AccName Property Description Address Town PostCode MyCo Head Office 1 MyStreet MyTown AB1 DEF AcmeLtd Workshop 1 MyRoad MyTown AB1 XYZ
  3. Pulsa ALT+F11 para abrir el Editor de Visual Basic, inserta un módulo y pega el código siguiente. La primera subrutina usa parámetros enlazados; la segunda muestra cómo construir un insert SOQL parametrizado como texto:
    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 db As DAO.Database Dim rs As DAO.Recordset Dim BlockCount As String Dim isPosted As Boolean RowCount = 0 ' Connect to the Easysoft Salesforce SOQL ODBC Driver data source con.Open "SFSOQL" comm.ActiveConnection = con ' One ? placeholder per column being passed in comm.CommandText = "insert into Account (Name, BillingStreet, BillingCity, " & _ "BillingPostalCode, Description ) values ( ?, ?, ?, ?, ? )" 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 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 con.BeginTrans ' start a new 200-row block isPosted = False End If BlockCount = BlockCount + 1 Debug.Print RowCount & " : " & rs.Fields("AccName") DoEvents 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 If BlockCount = 200 Then ' block full, post it Debug.Print "Block posted" con.CommitTrans isPosted = True BlockCount = 0 End If rs.MoveNext Loop rs.Close db.Close If Not isPosted Then con.CommitTrans ' flush the tail 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 db As DAO.Database Dim rs As DAO.Recordset RowCount = 0 con.Open "SFSOQL" SQLBase = "insert into Account (Name, BillingStreet, BillingCity, " & _ "BillingPostalCode, Description ) values ( " 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 con.BeginTrans isPosted = False End If BlockCount = BlockCount + 1 Debug.Print RowCount & " : " & rs.Fields("AccName") DoEvents 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 If BlockCount = 200 Then Debug.Print "Block posted" con.CommitTrans isPosted = True BlockCount = 0 End If rs.MoveNext Loop rs.Close db.Close If Not isPosted Then con.CommitTrans con.Close End Sub Function EscQuotes(inpStr As String) As String EscQuotes = Replace(inpStr, "'", "''") End Function
  4. Ejecuta cualquiera de las subrutinas desde el menú Run > Run Sub/UserForm y observa cómo se llena la organización de Salesforce en bloques de 200 filas.