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

Having problem creating an Attachment section
I have a page that lists a user's cases
The user can drill down on a case number and get to a page that displays the case details.
I want to add an Add an Attachment section.
I am probably doing some needless looping. I am missing the connection to the record that I need to add the comment to. The case number should be available to me since this page is the case number's details.
This is the error I am getting:
Visualforce Error
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Parent]: [Parent]
Class.RequestViewExtension.upload: line 53, column 1
Line 53 is: insert myAttachment;
Here is my page:
<apex:page standardController="Case" extensions="RequestViewExtension" tabStyle="My_Requests__tab">
<apex:stylesheet value="/resources/ForteStyles.css"/>
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Upload" action="{!Upload}" />
</apex:pageBlockButtons>
<html>
<div>
<apex:repeat value="{!cases}" var="s">
<hr />
Hello {!$User.FirstName}
<div style="clear:both;width:500px; height: 50px;">
<p style="font-size:1.2em; text-align:right; color: #1fbdf2; float:left; width: 30%;"> Request Number:</p>
<p style="font-size:1.2em; text-align:left;color:black; float:left; margin-left: 15px; width: 50%;"><apex:outputLink value="/apex/Viewed_Request?CaseNumber={!s.CaseNumber}">{! s.CaseNumber}</apex:outputLink></p>
</div>
<hr />
<div style="clear:both;width:500px; height: 50px;">
<p style="font-size:1.2em; text-align:right; color: #1fbdf2; float:left; width: 30%;"> Request Number:</p>
<p style="font-size:1.2em; text-align:left;color:black; float:left; margin-left: 15px; width: 50%;"> {!s.CaseNumber} </p>
<div style="clear:both;"></div>
</div>
<hr />
<div style="clear:both;width:500px; height: 50px;">
<p style="font-size:1.2em; text-align:right; color:#1fbdf2; float:left; width: 30%;"> Subject:</p>
<p style="font-size:1.2em; text-align:left;color:black; float:left; margin-left: 15px; width: 50%;">{!s.Subject}</p>
<div style="clear:both;">
</div>
</div>
<hr />
<div style="clear:both;width:500px; margin-bottom:20px; ">
<p style="font-size:1.2em; text-align:right; color:#1fbdf2; float:left; width: 30%;">Description:</p>
<p style="font-size:1.2em; text-align:left;color:black; float:left; margin-left: 15px; width: 60%;">{!s.Description}</p>
<div style="clear:both;">
</div>
</div>
<hr />
<div style="clear:both;width:500px; height: 50px;">
<p style="font-size:1.2em; text-align:right; color:#1fbdf2; float:left; width: 30%;">Create date:</p>
<p style="font-size:1.2em; text-align:left;color:black; float:left; margin-left: 15px; width: 50%;"> {!s.CreatedDate}</p>
<div style="clear:both;">
</div>
</div>
<hr />
<div style="clear:both;width:500px; height: 50px;">
<p style="font-size:1.2em; text-align:right; color:#1fbdf2; float:left; width: 30%;">Request Owner:</p>
<p style="font-size:1.2em; text-align:left;color:black; float:left; margin-left: 15px; width: 50%;"> <apex:outputField value="{!s.Ownerid}"/> </p>
<div style="clear:both;">
</div>
</div>
<hr />
<div style="clear:both;width:500px; height: 50px;">
<p style="font-size:1.2em; text-align:right; color:#1fbdf2; float:left; width: 30%;">Comment:</p>
<p style="font-size:1.2em; text-align:left;color:black; float:left; margin-left: 15px; width: 50%;"> <apex:outputField value="{!objCaseComment.CommentBody}"/> </p>
<div style="clear:both;">
</div>
</div>
<apex:inputHidden value="{!s.id}" id="fileParent"/>
<apex:pageBlockSection columns="2" collapsible="false" id="block1" title="Upload an Attachment">
<apex:pageBlockSectionItem >
<apex:outputLabel value="File Name" for="fileName"/>
<apex:inputText value="{!attachment.name}" id="fileName"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="File" for="file"/>
<apex:inputFile value="{!fileBody}" filename="{!fileName}" id="file"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Description" for="description"/>
<apex:inputTextarea value="{!fileDesc}" id="description"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:repeat>
</div>
</html>
</apex:pageBlock>
</apex:form>
</apex:page>
My Controller:
public class RequestViewExtension {
public Case cs;
ApexPages.StandardController controller;
public RequestViewExtension (ApexPages.StandardController con)
{
controller = con;
this.cs = (Case) con.getRecord();
}
public CaseComment objCaseComment{get; set;}
public list<Case> case1{get;set;}
public list<Case> getcases()
{
case1=[Select Priority,CaseNumber,(select CommentBody from CaseComments),id,Accountid,Subject,Description,Contactid,Ownerid,CreatedDate from case where CaseNumber=:ApexPages.currentPage().getParameters().get('CaseNumber')];
return case1;
}
public Attachment attachment {
get;
set;
}
public String Comment
{
get;
set;
}
public string fileName
{ get;set; }
public string fileParent
{ get;set; }
public Blob fileBody
{ get;set; }
public string fileDesc
{ get;set; }
public PageReference upload()
{
PageReference pr;
if(fileBody != null && fileName != null)
{
Attachment myAttachment = new Attachment();
myAttachment.Body = fileBody;
myAttachment.Name = fileName;
myAttachment.Description = fileDesc;
myAttachment.OwnerId = UserInfo.getUserId();
myAttachment.ParentId = fileParent ;
insert myAttachment;
pr = new PageReference('/' + myAttachment.Id);
pr.setRedirect(true);
return pr;
}
return null;
}
}
Some minor changes!
All Answers
You are not providing the parent Id
I just made the current record as the parent of the attachement
Thank you Avi,
I made the change but I am still getting:
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Parent]: [Parent]
Class.RequestViewExtension.upload: line 53, column 1
Some minor changes!
Avi, you are a rock star !
Thank you so much,
Haya