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;