1

I have an object - test_Object2__c for which i am creating a VF page. What I am trying to do is get an input from user for attachment and insert that attachment in notes and attachment related list of test_Object2__c. But I am having some trouble getting the attachment and insert it. Here is how I am going about it:

VF page:

<apex:page standardController="test_Object2__c" extensions="testobject2">

<apex:form >
<apex:commandButton action="{!savetest}" value="Save" id="theButton"/>
Name <apex:inputfield label="Name" value="{!testobj2.Name}"/>
Attachment <input type="file" id="myFile" value="{!attc}"/>
</apex:form>


</apex:page>

Extension:

public class testobject2 {

    public test_Object2__c testobj2{get;set;}
     public blob attc{get;set;}
    public testobject2(ApexPages.StandardController controller) {

    this.testobj2 = (test_Object2__c) controller.getrecord();

    }

    public pagereference savetest(){

    insert testobj2;
    system.debug('******************' + attc );

   /*** I can insert attachment using this:
    String myString = 'StringToBlob';
    Blob myBlob = Blob.valueof(myString);
    Attachment a = new Attachment(parentid=testobj2.id, Name = 'xyz' , Body = myBlob );
    insert a;
    ***/
    Attachment a = new Attachment(parentid=testobj2.id, Name = 'xyz' , Body = attc );
    insert a;
    return null;
    }

}

I get a null pointer exception at line where I am creating a new Attachment object. Any suggestion on how I can get an attachment from user and save it while the test_Object2__c is saved?

When creating through VF page: enter image description here

When adding manually by uploading file in notes and attachment( standard salesforce functionality)

enter image description here

1 Answer 1

4

Declare an Id variable. In the constructor, retrieve the Id of the record as follows:

controller.getId();

With this Id, try to insert the attachment record.

Change VFP code like this:

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

One more point use attachment code like this,

private Attachment myfile;
public Attachment getmyfile() {
    myfile = new Attachment();
    return myfile;
}

Use body = myfile.body at the line inserting the attachment

Attachment a = new Attachment(parentid=testobj2.id, Name = myfile.name , Body = myfile.body );

You can refer this post: Link upload file to Lead Record

9
  • I guess I have testobject2.id so I don't have to getid() again. I think something is wrong with attachment - the way I am getting the attachment
    – SfdcBat
    Jul 7, 2016 at 20:02
  • @SfdcBat check my answer, it should work Jul 7, 2016 at 20:19
  • Thanks. I tried this and able to attach. But the document I attach , the format is not getting saved. for ex- if I attach a word doc, it does not show as .doc attachment. It rather show some file attachment which has no format. When I download it and try to open I have to select the open with option to view the file. Is there a way to store the format in this attachment?
    – SfdcBat
    Jul 7, 2016 at 21:15
  • 1
    @SfdcBat. let me try it out Jul 7, 2016 at 21:17
  • @SfdcBat I have updated the code and checked in my dev org, it is working fine Jul 7, 2016 at 21:25

You must log in to answer this question.

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