Connecting Delphi to Salesforce

unit Salesforce;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf,
  FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async,
  FireDAC.Phys, FireDAC.Phys.ODBC, FireDAC.Phys.ODBCDef, FireDAC.FMXUI.Wait,
  FireDAC.DApt, FMX.Controls.Presentation, FMX.StdCtrls, Data.DB,
  FireDACp.Client, FMX.ScrollBox, FMX.Memo;

type
  TForm1 = class(TForm)
    FDConnection1: TFDConnection;
    connectButton: TButton;
    outputMemo: TMemo;
    executeButton: TButton;
    procedure connectButtonClick(Sender: TObject);
    procedure executeButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.connectButtonClick(Sender: TObject);
begin
  outputMemo.Text := '';

  try
    // Establish the connection.
    FDConnection1.Open;
    executeButton.Enabled := True;
    outputMemo.Lines.Add('Connection established!');
  except
    on E: EDatabaseError do
      outputMemo.Lines.Add('Exception raised with message' + E.Message);
  end;
end;

procedure TForm1.executeButtonClick(Sender: TObject);
var
  query: TFDQuery;
begin

  query := TFDQuery.Create(nil);

  try
    // Define the SQL Query
    query.Connection := FDConnection1;
    query.SQL.Text := 'SELECT * FROM Account';
    query.Open();
    outputMemo.Text := '';
    // Add the field names from the table.
    outputMemo.Lines.Add(String.Format('%s | %s', [' ID ', ' NAME ']));
    // Add one line to the memo for each record in the table.
    while not query.Eof do
    begin
      outputMemo.Lines.Add(String.Format('%s | %s',
        [query.FieldByName('ID').AsString, query.FieldByName('Name').AsString]));
      query.Next;
    end;

  finally
    query.Close;
    query.DisposeOf;
  end;

end;

end.

The Salesforce ODBC driver enables you to work with Salesforce data in Embarcadero Delphi applications.

The Salesforce ODBC driver is available to download from the Easysoft web site:

  1. Download the Windows Salesforce ODBC driver.
  2. Install and license the Salesforce ODBC driver on the machine where is installed.

    For installation instructions, refer to the Salesforce ODBC driver documentation.

Before you can use the Salesforce ODBC driver to connect Delphi to Salesforce, you need to configure an ODBC data source. An ODBC data source stores the connection details for the target database (in this case, Salesforce) and the ODBC driver that is required to connect to it (in this case, the Salesforce ODBC driver).

We are going to create a 32-bit Delphi application, so we use the 32-bit version of ODBC Data Source Administrator (%WINDIR%\SysWOW64\odbcad32.exe) to configure our data source.

To create a Salesforce ODBC driver data source:

  1. Do one of the following:
    • To create a user data source, in the User DSN tab, choose Add.

      –Or–

    • To create a system data source, choose the System DSN tab, and then choose Add.
  2. In the Create New Data Source dialog box, choose Easysoft ODBC-Salesforce Driver, and then choose Finish.
  3. Complete the DSN Setup dialog box:
    Setting Value
    DSN Salesforce
    User Name The name of your Salesforce user. For example, myuser@mydomain.com.
    Password The password for your Salesforce user.
    Token The security token for your Salesforce user, if required.

    To find out whether you need to supply a security token, choose the Test button. If the connection attempt fails with an error which contains LOGIN_MUST_USE_SECURITY_TOKEN, you need to supply one.

    Salesforce emails the security token to the email address associated with your Salesforce user account. If you have not received a security token, you can regenerate it. Salesforce will then email the new security token to you. To regenerate your security token, log in to Salesforce and then choose Setup from the user menu. Search for "security token" in the Quick Find box. Choose Reset Security Token in the Reset Security Token page. When you receive the token in your email client, copy it and then paste it into the Token field.

  4. Use the Test button to verify that you can successfully connect to Salesforce.

Follow these steps to query some Salesforce data from a Delphi application.

  1. In a new Delphi project, add a form
  2. Add two TButton controls, named executeButton and connectButton. Set the Text properties of the buttons to Execute and Connect.
  3. Set the Enabled property of executeButton to False.
  4. Add a TMemo control named outputMemo.
  5. Add a TFDConnection component. Double-click this component.

    The FireDAC Connection Editor dialog box is displayed.

  6. In the Driver ID list, choose ODBC.
  7. Choose the Wizard button. Choose your Salesforce data source from the Machine Data sources tab, when prompted.
  8. In the FireDAC Connection Editor dialog box, delete the values from the User_Name and Password boxes.

    In the ODBCAdvanced box, append this string to the existing value:

    UID=user_name;PWD=password.

    Replace user_name and password with the values you just deleted from the User_Name and Password boxes.

  9. Add this code to the OnClick event handler for connectButton:
    procedure TForm1.connectButtonClick(Sender: TObject);
    begin
      outputMemo.Text := '';
    
      try
        // Establish the connection.
        FDConnection1.Open;
        executeButton.Enabled := True;
        outputMemo.Lines.Add('Connection established!');
      except
        on E: EDatabaseError do
          outputMemo.Lines.Add('Exception raised with message' + E.Message);
      end;
    end;
  10. Add this code to the OnClick event handler for executeButton:
    procedure TForm1.executeButtonClick(Sender: TObject);
    var
      query: TFDQuery;
    begin
    
      query := TFDQuery.Create(nil);
    
      try
        // Define the SQL Query
        query.Connection := FDConnection1;
        query.SQL.Text := 'SELECT * FROM Account';
        query.Open();
        outputMemo.Text := '';
        // Add the field names from the table.
        outputMemo.Lines.Add(String.Format('%s | %s', [' ID ', ' NAME ']));
        // Add one line to the memo for each record in the table.
        while not query.Eof do
        begin
          outputMemo.Lines.Add(String.Format('%s | %s',
            [query.FieldByName('ID').AsString, query.FieldByName('Name').AsString]));
          query.Next;
        end;
    
      finally
        query.Close;
        query.DisposeOf;
      end;
    
    end;
  11. Compile and run the application.
  12. To connect to the Salesforce data source, choose the Connect button. To return some data from the Account table, choose the Execute button.