function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
OTOT 

Getting attachments through the Sforce api

Hi,

Currently we use the sforce api simple query to get opportunities, contacts and accounts and import them into one of our internal systems. There has now been a request to bring over any attachments on opportunities as well. I used the simple query on the attachment object but the body was encoded. How can I bring over the actual attachments?

Any help would be greatly appreciated!

Lisa

SIlgenfritzSIlgenfritz

This function I use to get the attachments for an opportunity and send an email to our Lotus notes system to use in another appilcation.  I basically get the attachment and write it out to a temporary directory and then attach it using dime to my new email message.  Hope this helps.


Private Function SendMail(ByVal iControlNumber As Integer, ByVal OpportunityID As String) As Boolean

Dim sfHeader As New SalesForceCom.SessionHeader

Dim sfQryRslt As QueryResult

Dim BinObj() As Byte

Dim Attch As Attachment

Dim sfobjects() As sObject

Dim sAttIds() As String

Dim OutStrm As Stream

Dim sPath As String

Dim sFullFile As String

Dim sFileList As New ArrayList

Dim x As Integer = 0

Dim Ct As Integer = 0

Dim MailAttach As New Microsoft.Web.Services2.Dime.DimeAttachmentCollection

Dim QryString As String = "Select ID from Attachment Where ParentID = '" & OpportunityID & "'"

Dim DimeAttach As DimeAttachment

Dim ctxt As SoapContext = SFIntSvc.RequestSoapContext

 

ctxt.Attachments.Clear()

sfQryRslt = SFSvc.query(QryString)

If sfQryRslt.size > 0 Then

ReDim sAttIds(sfQryRslt.size - 1)

For y As Integer = 0 To sfQryRslt.size - 1

Attch = sfQryRslt.records(y)

If Attch.Id Is DBNull.Value Or Attch.Id <> String.Empty Then

sAttIds(y) = Attch.Id

End If

Next

 

sfobjects = SFSvc.retrieve("Id, Body, BodyLength, CreatedById, CreatedDate, IsPrivate, Name, OwnerId, ParentID", "Attachment", sAttIds)

If sfobjects.Length > 0 Then

Attch = CType(sfobjects(0), Attachment)

sPath = ConfigurationSettings.AppSettings.Get("SLF.AttachmentPath") & Attch.ParentId & "\"

DeleteExistingAttachments(Attch.ParentId.Trim, sPath)

Directory.CreateDirectory(sPath)

For Each obj As sObject In sfobjects

Attch = CType(obj, Attachment)

sFullFile = sPath & Attch.Name

sFileList.Add(sFullFile)

'Write file

OutStrm = File.Open(sFullFile, FileMode.Create)

BinObj = Attch.Body

OutStrm.Write(BinObj, 0, Attch.BodyLength)

OutStrm.Close()

'Add to attachements array

DimeAttach = GetDimeAttachment(sFullFile)

If Not DimeAttach Is Nothing Then

ctxt.Attachments.Add(DimeAttach)

End If

OutStrm = Nothing

Next

End If

End If

'send email with attachments

SFIntSvc.SendEmailAttachments(NumberToAS400(iControlNumber, 6, 0))

Try

For f As Integer = 0 To sFileList.Count - 1

File.Delete(sFileList(x).ToString)

Next

If Directory.Exists(sPath) Then

Directory.Delete(sPath)

End If

Catch ex As Exception

WriteToLog(ex)

End Try

End Function


SuperfellSuperfell
The body is base64 encoded over the wire (as required by XML), but .NET will automatically translate that back to an array of bytes for you, so you're body property is an array of bytes of the attachment. If you want it in a file, then it's easy from there to create a new filestream and write the byte array to it.