You need to sign in to do that
Don't have an account?
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.
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
}
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.
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
Can you please provide that code please .
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.
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