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
CarlSpoonCarlSpoon 

Download and Upload Attachment Sample Code in VB.Net

Does any one have a sample code in VB.net on how to download/upload attachments from a customer controller (NotesAndAttachments relatedlist)?
CarlSpoonCarlSpoon

I saw a sample code in Php which I modify to VB.NET; however, the Body and BodyLength of the attachment object are always equal to Nothing, even though the file in SalesForce has a file size.

 

Here is the sample code:

Dim sqlstr As String = "Select p.CreatedBy.Name, p.CreatedDate, p.Id, p.ParentId, p.OwnerId, p.IsDeleted, p.Name,p.LastModifiedBy.Name From Attachment p Where p.CreatedDate > " & strLastRunDate

 

Dim qr As QueryResult = Nothing

qr = binding.queryAll(sqlstr)

Dim MaxRec as Integer = qr.records.Length

 For i = 0 To MaxRec - 1

   prAttchObj = qr.records(i)

   

   'Problem!! prAttchObj.Body is always  = Nothing

   If Not IsNothing(prAttchObj.Body) Then

       'NewRec.FileBody is of type Byte[]

       newRec.FileBody = prAttchObj.Body

       .

       .

       . 

    End If

Next

SuperfellSuperfell
Body & BodyLength will be Nothing because you didn't include those fields in your SOQL query to fetch the data.
CarlSpoonCarlSpoon

Here is the definition of the object:

Dim prAttchObj As SandBox.Attachment

CarlSpoonCarlSpoon
Thanks.  Simple mistake.