+ Start a Discussion
Swathi JainSwathi Jain 

I want to put attachments in pages.

i am newto code, 

I have this code 

<apex:page standardController="Custom_Object__c" extensions="attachmentsample">

.

.Somecode here

.

.

<apex:pageblockSection title="Attach Documents" collapsible="false">

<apex:inputfile value="{!myfile.body}" filename="{!myfile.Name}" />
<apex:commandbutton value="Attachfiles" action="{!Savedoc}"/>
</apex:pageblockSection>

 

</apex:page>

 

public class attachmentsample {

public attachmentsample(ApexPages.StandardController controller) {

}
Public Attachment myfile;
Public Attachment getmyfile()
{
myfile = new Attachment();

return myfile;
}

Public Pagereference Savedoc()
{
String ldid = System.currentPagereference().getParameters().get('id');
Attachment a = new Attachment(parentId = ldid, name=myfile.name, body = myfile.body );
insert a;
return NULL;
}

}

The above code is working for Editing the custom object page not for New page, please help me for this.

I want to use when creating the custom object and editing the custom object. 

Starz26Starz26

You need to save the current record if new before you can get the ID

 

for example:

 

Account a {get;set;}

 

//constructor

public void thePage(ApexPages.StandardController c){

 

      a = (Account)c.getRecord();

 

}

 

public void saveDoc(){

 

  if(a.id == Null)

         insert a;

 

  //do doc save stuff using a.id as parentid

 

}

Swathi JainSwathi Jain

Hey Starz26,

 

I am pretty new to code, please Can you please update the code! Sorry to ask this. please update the complete class code.


Starz26 wrote:

You need to save the current record if new before you can get the ID

 

for example:

 

Account a {get;set;}

 

//constructor

public void thePage(ApexPages.StandardController c){

 

      a = (Account)c.getRecord();

 

}

 

public void saveDoc(){

 

  if(a.id == Null)

         insert a;

 

  //do doc save stuff using a.id as parentid

 

}


 

Starz26Starz26

Take a stab at it.... I gave you the parts to put it in, you just need to translate the places they go in your controller

Swathi JainSwathi Jain

Can you please provide that code please .


Starz26 wrote:

Take a stab at it.... I gave you the parts to put it in, you just need to translate the places they go in your controller


 

Devendra@SFDCDevendra@SFDC

 

1) In Order to insert attachment, you first neet to insert its Parent Record,

 

For example,

 

If you are looking to insert attachment record for Account object then you will have to first insert Account record. The Id of Account record act as a parentId at the time of inserting attachment.

 

Attachment a = new Attachment(parentId = accid, name=myfile.name, body = myfile.body);

ParentId will be inserted AccountId.

 

2) When you create new attachment for any Account record, then you should pass the Id of that Account. So that this Id will be used as parentId for your attachment.

 

Hope this helps :)

 

Thanks,

Devendra