+ Start a Discussion
SimpleJoeSimpleJoe 

How to see attachments to Custom object from APEX class

Hi all.

 

I'm need help.I need to create list of attachments of custom objects for VisualForce page, but how I can get access to attachments of object. In the manual I not found information about acces to attachments from an APEX code.

 

Please Help.

aalbertaalbert

I have used a combination of an <apex:inputFile> tag in the Visualforce page with Attachment SObject in Apex.

 

Sample Page

 

<apex:page controller="AttachmentExt" > <apex:messages /> <apex:form id="theForm"> <apex:pageBlock > <apex:pageBlockSection > <apex:inputFile value="{!a.body}" filename="{!a.name}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>

 

Sample controller:

 

public class AttachmentExt { public Attachment a{get;set;} }

 

And then you can use Apex DML statements (insert, update, ect) to create the Attachment and associate it to a parent record (ie Custom Object). 

 

 

 

 

 

 

nrwingnrwing

aalbert,

 

Can I automate the attachment on a standard object using Apex without using Visualforce ( I am not up to speed on Visualforce yet )?  I have it going through the hoops, but the file that ends up being attached is not what's intended.  Instead it is just a text file of the 'blob' data type, not the Excel file.  Is there a trick to loadinga blob datatype so that it will recognize the format of the file desired? 

 

Any help greatly appreciated....stumped at the moment.

aalbertaalbert

How is the attachment being loaded into salesforce.com?

What is the Content Type set to?

What are you trying to do that is different than the standard attachment process? 

nrwingnrwing

I am loading an attachment to a Case in an After Update Trigger when that case is a certain record type......

 

I loaded the attachment manually first, then exported the 'body' of that, and used that text string as the new insert value on the auto-attached attachment body.

 

I also used the content type of that record (application/vnd.ms-excel) .......... to load into that value.....

 

The string/blob for the body of the attachment is a really long character string, so I believe it is converted correctly....

 

Thanks,

nrwingnrwing

It will attached a file, but i get "file is not in format specified by extension" and when you try to download it, while it will open in excel, it just has the long character string in the A1 cell.....nothing else......

 

so while i can load a file, for some reason, I cannot get it to load in a correctly recognized format so the viewing/downloading it is also working....

nrwingnrwing
I think I might have the issue.  I don't think I had the correct string in there (base64).  I was using what was exported from Salesforce, and that was probably not correct....... will post if I get this to work....
nrwingnrwing

Still no good.  The file loaded, when exported, is just a string of characters when opened......  I am wondering if this can even be done this way.  I have seen notes throughout the boards saying it can be, but nothing that says it has been.  Any othe thoughts on this ? Or have you seen anything anywhere else?

 

Thanks for any help....

 

SimpleJoeSimpleJoe

Hi aalbert.

 

Thanks for the answer, but I dont know nothing about sObject Attachment. In the  manual not nothing.

Where possible to read about properties and metods of this class. I need to read and select attachments from my custom objects.How?

 

Thanks for answer.

aalbertaalbert

Here is a link to the API doc that includes the schema definitions: Link

Also, in your Force.com IDE, open up the schema explorer which also shows all the properties of the Attachment object. 

 

workaholicmomworkaholicmom

Hello,

 

I did not ask the original question that this reply comes from.  However, I have not seen a reply to my similar question so I am going to provide detail to your questions.

 

I am attaching documents to a Custom Object's Related List (Attachments, etc.)

I want the attachment to show up in the Account related list the way that attaching to an Opportunity would.

 

The Custom Object has a LookUp Relationship with the Account record.