0

TestCompone.page

<apex:page standardController="WebformData__c" extensions="TestCompon">
<apex:form >
<apex:outputLabel >Pick One</apex:outputLabel><br/>
<apex:selectList id="po" value="{!svalue}" size="1" required="TRUE" multiselect="FALSE">
    <apex:selectOptions value="{!otherOpt}"/>
    <apex:actionSupport event="onchange" action="{!onChangeFnCall}" rerender="pg"/>
</apex:selectList>

<apex:outputPanel id="pg">
  <apex:outputPanel rendered="{!pgRendered}">
    <div>
      <c:testcompo values="{!svalue}"/>
    </div>
  </apex:outputPanel>
</apex:outputPanel>
</apex:form>
</apex:page>

testcompo.component

 <apex:component controller="TestCompon" allowDML="true">
 <apex:attribute name="values" type="String" description="picklistvalue" required="TRUE" assignTo="{!plvalue}"/>
    <br/><br/>
<div>
      <apex:outputLabel value="file" for="file"/>
      <apex:inputFile value="{!file.body}" filename="{!file.name}" id="file"/>
  </div>
<apex:outputLabel escape="false" value="<b>Enter Data</b>" style="white-space: pre" for="sDataId"/>
<apex:inputtext style="width: 360px;" id="sDataId" value="{!sdata}"/><br/><br/><br/>

 <div class="center">
     <apex:commandButton styleClass="TabSpace" action="{!save}" value="Save" />
 </div>
</apex:component>

TestCompon class

public Class TestCompon{
  public boolean pgRendered {get;set;}
  public String svalue;
      public void setsvalue(String record) {
        svalue = record;
      }
      public String getsvalue() {
         return svalue;
      }
    public Attachment file {
    get {
        if (file == null)
          file = new Attachment();
        return file;
      }
    set;
    }
  public List<SelectOption> otherOpt;
  public String plvalue {get;set;}
  public String sdata {get;set;}


  public TestCompon() {
  }

 public TestCompon(ApexPages.StandardController controller) {
    otherOpt = new List<SelectOption>();
  }
  public List<SelectOption> getOtherOpt() {
    Schema.DescribeFieldResult description = WebformData__c.otherOptions__c.getDescribe();
    for (Schema.Picklistentry pl : description.getPicklistValues())
   {
        otherOpt.add(new SelectOption(pl.getValue(),pl.getLabel()));
    }
    return otherOpt;
  }

  public void onChangeFnCall(){
     if(getsvalue()=='HPC'){
         pgRendered = TRUE;
         System.debug('svalue' + getsvalue());

     }
   }
     private WebformData__c buildForm(){
      WebformData__c form = new WebformData__c();
      form.Sensitive_Data__c = sData;
      return form;
    }
   public PageReference save(){
      WebformData__c form = buildForm();
      try{
       // code to save the attachment
        insert form;
        Attachment a = new Attachment(parentId = form.Id, name=file.name, body = file.body);
        System.debug('attachment.name: ' + attachment.name);// Prints NULL
        System.debug('attachment.body: ' + attachment.body);// prints NULL

        insert a; // Exception occurs here since name and body are null why?
      }catch(Exception ex){
        system.debug(ex);
      }
      return null;
    }

}

Exception:

00:02:56:029 VARIABLE_ASSIGNMENT 
  [60]|ex|"common.apex.runtime.impl.DmlExecutionException: Insert failed.
  First exception on row 0; first error: REQUIRED_FIELD_MISSING, 
  Required fields are missing: [otherOptions]: [otherOptions]"|0x261632fd
9
  • I can't remember attachment have any field name "otherOptions". Do you have any trigger on them? Jun 28 '18 at 7:20
  • Does your object WebformData__c have a field named otherOptions? If yes, add a value to that field in your buildForm method.
    – S..
    Jun 28 '18 at 7:25
  • @N.B, Yes WebformData__c has a field otherOptions. Error occurs inserting the attachment. attachment name and body prints null when I insert debuglogs
    – Mentee
    Jun 28 '18 at 7:31
  • 3
    You need to add a value for otherOptions before inserting form. As per the error otherOptions is a required field on object WebformData__c and since it is not filled, you are not able to insert form properly. Then you are using form.Id as parentId in Attachment, that's why it is failing.
    – S..
    Jun 28 '18 at 8:07
  • 1
    Thanks @N.B and Tushar Sharma,, I released my blunder mistakes 1) I have File variable on the top and created a new attachment and trying to insert it 2) I made other options filed while pasting the code here i forgot to remove it as a required field. 3) I pasted the wrong error. Thanks issue is RESOLVED
    – Mentee
    Jun 28 '18 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.