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
HayaHaya 

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]

 
Error is in expression '{!Upload}' in page viewed_request_attachment

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; 
    } 
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

Some minor changes!

 

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 && !case1.isEmpty()) {
            Attachment myAttachment = new Attachment();
            myAttachment.Body = fileBody;
            myAttachment.Name = fileName;
            myAttachment.Description = fileDesc;
            myAttachment.OwnerId = UserInfo.getUserId();
            myAttachment.ParentId = case1[0].Id;
            insert myAttachment;
            pr = new PageReference('/' + myAttachment.Id);
            pr.setRedirect(true);
            return pr;
        }
        return null;
    }
}

All Answers

Avidev9Avidev9

You are not providing the parent Id

 

I just made the current record as the parent of the attachement

 

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 = cs.Id;
            insert myAttachment;
            pr = new PageReference('/' + myAttachment.Id);
            pr.setRedirect(true);
            return pr;
        }
        return null;
    }
}

 

HayaHaya

Thank you Avi,

 

I made the change but I am still getting:

 

Visualforce 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 '{!Upload}' in page viewed_request_attachment

 

 

Class.RequestViewExtension.upload: line 53, column 1

Avidev9Avidev9

Some minor changes!

 

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 && !case1.isEmpty()) {
            Attachment myAttachment = new Attachment();
            myAttachment.Body = fileBody;
            myAttachment.Name = fileName;
            myAttachment.Description = fileDesc;
            myAttachment.OwnerId = UserInfo.getUserId();
            myAttachment.ParentId = case1[0].Id;
            insert myAttachment;
            pr = new PageReference('/' + myAttachment.Id);
            pr.setRedirect(true);
            return pr;
        }
        return null;
    }
}
This was selected as the best answer
HayaHaya

Avi, you are a rock star  !

Thank you so much,

Haya