0

I am making a custom form with the standard Case controller that needs to be able to accept attachments. I have written a controller extension that does a couple of things, including handle the attachment. The problem I am running into is when i go to insert the attachment. The error states: "Insert Failed REQUIRED_FIELD_MISSING, Required fields are missing: [Parent]: [Parent]".

I have, however, set the parentId for this attachment when I write: ParentId = supportCase.Id. Below is teh code for the controller extension:

public class PartnerSupportCaseController {

public Case supportCase {get;set;}

public Attachment attachedFile;



public PartnerSupportCaseController(ApexPages.StandardController controller)
{

    this.supportCase = (Case)controller.getRecord();
    supportCase.Protocol_Sub_Issue__c = 'Partner Support - Please Pick';
    supportCase.Protocol_Issue__c = 'Partner Support - Please Pick';
    supportCase.RecordTypeId = '012M000000092ju';


}

public Attachment getAttachedFile(){
    attachedFile = New Attachment();
    return attachedFile;

}



public pagereference Save()
{


    Attachment a = new Attachment(ParentId = supportCase.Id, name = attachedFile.Name, body = attachedFile.Body);

    insert supportCase;
    insert a;      

    PageReference pg = Page.PartnerSupportThankYou;
    pg.setredirect(true);
    return pg;


  }


 } 

I am also adding the excerpt from my form where the file is taken in:

<apex:page sidebar="false" showHeader="false" standardController="Case" extensions="PartnerSupportCaseController">

<apex:pageBlockSection>

           <apex:inputfile value="{!attachedFile.body}" filename="{!attachedFile.Name}" />                       
           </apex:pageBlockSection>

I just need to pinpoint why this insert is failing due to a missing required field, even though I have filled that required field.

Thanks,
-CP

2
  • are you passing case id in url ?
    – Himanshu
    Oct 26, 2015 at 18:59
  • In the form URL? I am not.
    – Chris P
    Oct 26, 2015 at 19:03

1 Answer 1

1

putting this as answer because if I put that as comment you might confuse with syntax.

you have defined a visualforce page and until and unless you are not passing id of case in url controller will not get it, Try to get one case id in your org and pass this in url in following way.

/apex/YOURVFPAGENAME?id=CASERECORDID

replace both parameters.

  1. YOURVFPAGENAME with your visualforce page name
  2. CASERECORDID with any case id.

if you want to create a case everytime you upload attachment update your save method in following way

public pagereference Save()
{

   upsert supportCase;
   Attachment a = new Attachment(ParentId = supportCase.Id, name = attachedFile.Name, body = attachedFile.Body);
   insert a; 

} 
8
  • Thanks, I'm doing that now. Do you mean, in this instance, substituting CASERECORDID with supportCase.Id? This needs to create a case every time as opposed to update one.
    – Chris P
    Oct 26, 2015 at 19:10
  • are you creating case everytime you add attachment ?
    – Himanshu
    Oct 26, 2015 at 19:12
  • no, every time this form is filled out, and then redirected to the "thank you" page, a new case should be created. Attachments will only sometimes be included (as attachments is an optional field on the form)
    – Chris P
    Oct 26, 2015 at 19:13
  • this error was coming because you were referring the supportcase.id prior to inserting the case record which you were creating support case. which was making supportcase.id in Attaachment a line.
    – Himanshu
    Oct 26, 2015 at 19:15
  • Ah ok. would I need to write code to index it or move that bit to after "insert supportCase;?
    – Chris P
    Oct 26, 2015 at 19:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .