You need to sign in to do that
Don't have an account?

Uploading a file in custom object
I have created one custom object and i got this error
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Parent]: [Parent]
Class.VFFileUpload.UploadFile: line 26, column 1
-------------------------------------------------------------------------------
public class VFFileUpload
{
public Id recId
{ get;set; }
public VFFileUpload(ApexPages.StandardController ctlr)
{
recId = ctlr.getRecord().Id;
}
public string fileName
{ get;set; }
public Blob fileBody
{ get;set; }
public PageReference UploadFile()
{
PageReference pr;
if(fileBody != null && fileName != null)
{
Attachment myAttachment = new Attachment();
myAttachment.Body = fileBody;
myAttachment.Name = fileName;
myAttachment.ParentId = recId;
insert myAttachment;
pr = new PageReference('/' + myAttachment.Id);
pr.setRedirect(true);
return pr;
}
return null;
}
}
-------------------
<apex:page standardController="MyCustomObject__c" extensions="VFFileUpload">
<apex:form>
<apex:pageBlock title="Upload Attachment">
<apex:inputFile style="width:100%" id="fileToUpload" value="{!fileBody}" filename="{!fileName}" />
<apex:commandButton value="Upload Attachment" action="{!UploadFile}"/>
</apex:pageBlock>
</apex:form>
</apex:page>
@venchinn2,
Did you check whether you have Id in recId that you are assigning to ParentId in Attachment?
Well you can optimize your code a bit. Have a checking for parentId or using try catch
Main cause : Probably you are not passing the record Id to the page. Page URL Should be /apex/VFFileUploadPage?id=<MyCustomObject__c.Id>
Replace <MyCustomObject__c.Id> with a record ID
Hi avi,
Variable does not exist: fileBody at line 22 column 13
i got his error
Well that should be easy!!!
Closely look at the code.
I somehow left a piece of old code there
should be
Hi Avi,
Againn i am getting some error like method doesnot exist and my requirement is when i save a doc in vf page it will save into my documents as well as in my custom object tell me how will i do please?