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
venchinn2venchinn2 

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]

Error is in expression '{!UploadFile}' in component <apex:commandButton> in page vffileupload

 

 

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>

jiah.choudharyjiah.choudhary

@venchinn2,

Did you check whether you have Id in recId that you are assigning to ParentId in Attachment?

Avidev9Avidev9

Well you can optimize your code a bit. Have a checking for parentId or using try catch

 

public class VFFileUpload {
    public Id recId {
        get;
        set;
    }

    public VFFileUpload(ApexPages.StandardController ctlr) {
        recId = ctlr.Id();
      	myAttachment = new Attachment();
    }

    public Attachment myAttachment {
        get;
        set;
    }
	

    public PageReference UploadFile() {
        PageReference pr;
        if (fileBody != null && fileName != null) {
          try{
            myAttachment.ParentId = recId;
            insert myAttachment;           
            pr = new PageReference('/' + myAttachment.Id);
            pr.setRedirect(true);
            return pr;
          }
          catch(Exception ex){
          	Apexpages.addmessages(ex);
          }
        }
		myAttachment = new Attachment(); //incase of error reset the variable to avoid view state errors
        return pr;
    }
}

 

 

<apex:page standardController="MyCustomObject__c" extensions="VFFileUpload">
    <apex:form>
        <apex:pageBlock title="Upload Attachment">
            <apex:inputFile style="width:100%" id="fileToUpload" value="{!myAttachment.Body}" filename="{!myAttachment.Name}" />
            <apex:commandButton value="Upload Attachment" action="{!UploadFile}" />
        </apex:pageBlock>
    </apex:form>
</apex:page>

 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

 

 

venchinn2venchinn2

Hi avi,

 

Variable does not exist: fileBody at line 22 column 13 

 

i got his error

Avidev9Avidev9

Well that should be easy!!!

Closely look at the code.

 

if (fileBody != null && fileName != null) {

I somehow left a piece of old code there

 

should be 

 

if (myAttachment.Body != null && myAttachment.Name != null) {

 

venchinn2venchinn2

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?

 

Avidev9Avidev9
I will sugges to go through the code a bit. THe error you are getting can be easily solved. and I guess your requirement or may be the boeard question says "Uploading a file in custom object"