Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
Delphi

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

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;
var
SS: TStringStream;
MS: TMemoryStream;
begin
  Result := '';

  MS := TMemoryStream.Create;
  SS := TStringStream.Create;
  ABitmap.SaveToStream(MS);
  MS.Position := 0;
  TNetEncoding.Base64.Encode(MS,SS);
  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;
  FDQuery1.Open;
  FDQuery1.First;
  if FDQuery1.RecordCount>0 then
    begin
      Result := FDQuery1.FieldByName('Id').AsString;
    end;

  SS.Free;
  MS.Free;
end;

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


 


See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

FMXExpress.com has over 600 articles with all kinds of tips and tricks for Delphi FireMonkey on Android, IOS, OSX, Windows, and Linux.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES