Upload An Attachment To SalesForce With The Delphi 10.2 Tokyo Enterprise Connectors

by Aug 8, 2018

I am working on a project that utilizes the Embarcadero Enterprise Connectors powered by CData to access SalesForce through it's API. The Enterprise Connectors provide you a wide variety of connectors for quickly and easily accessing APIs like SalesForce, Google AdWords, Mailchimp, Facebook, Azure, YouTube, and many many more.

The Enterprise Connectors work through FireDAC and give you an SQL interface to the various APIs that are on offer. This means you don't have to learn new APIs you simply use standard SQL to access all of the data that you need.

In the case of SalesForce one of the requirements of the project is to upload attachments and attach them to an existing item in SalesForce. I created this function which shows how to do this quickly and easily. SalesForce takes files as a Base6 encoded string so I utilized the System.NetEncoding unit to do this.

There are a number of different ways to write the below code and this is just one way. You'll have to add your own try..finally or try..except statements to make it more boilerplate.

function TForm1.UploadAttachment(const AObjectId, AFileName: String; ABitmap: TBitmap): String;
SS: TStringStream;
MS: TMemoryStream;
  Result := '';

  MS := TMemoryStream.Create;
  SS := TStringStream.Create;
  MS.Position := 0;
  SS.Position := 0;

  FDQuery1.SQL.Text := 'EXEC CData.Salesforce.UploadAttachment `ObjectId` = :ObjectId, `Base64Data` = :Base64Data, `FileName` = :FileName';
  FDQuery1.ParamByName('ObjectId').AsString := AObjectId;
  FDQuery1.ParamByName('Base64Data').AsMemo := SS.DataString;
  FDQuery1.ParamByName('FileName').AsString := AFileName;
  if FDQuery1.RecordCount>0 then
      Result := FDQuery1.FieldByName('Id').AsString;


Find out more about the Enterprise Connectors and how you can utilize them in your Delphi and C++ apps using FireDAC.