Easysoft Blog

Wed, 26 July 2017

Connecting Delphi to Salesforce.com

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,
  FireDAC.Comp.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.com ODBC Driver enables you to work with Salesforce.com data in Embarcadero Delphi applications.

The Salesforce.com ODBC Driver is available to download from the Easysoft web site:

  1. Download the Windows Salesforce.com ODBC Driver. (Registration required.)
  2. Install and license the Salesforce.com ODBC Driver on the machine where is installed.

    For installation instructions, see the Salesforce.com ODBC Driver documentation.

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

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

To create a Salesforce.com 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 Easysoft ODBC-Salesforce Driver DSN Setup dialog box:
    SettingValue
    DSNSalesforce.com
    User NameThe name of your Salesforce.com user. For example, myuser@mydomain.com.
    PasswordThe password for your Salesforce.com user.
    TokenThe security token for your Salesforce.com 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.com emails the security token to the email address associated with your Salesforce.com user account. If you have not received a security token, you can regenerate it. Salesforce.com will then email the new security token to you. To regenerate your security token, log in to Salesforce.com and then choose Setup from the user menu. Search for "security token" in the Quick Find box. Click 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.com.

Follow these steps to query some Salesforce.com 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, 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.com data source, choose the Connect button. To return some data from the Account table, choose the Execute button.