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
David Marshall 10David Marshall 10 

Optional Document Upload on Visualforce Page

I have a Visualforce page we use as a registration step for perspective clients. Currently, we use this page to capture some generic information from the client. However, we want to expand the form to allow for documents to be uploaded, which will then be attached to the Lead record that is being created. I have found code to support this; however, when implemented, it works fine if I attach both documents that I have alloted for, but if only one, or none, is attached, then the code halts after the attachment section.

Code Snippet:
public SWWRegister_Controller(ApexPages.StandardController controller) {}
    Public Attachment myfile;
    Public Attachment getmyfile()
    {
        myfile = new Attachment();
        return myfile;
    }
    Public Attachment myfile2;
    Public Attachment getmyfile2()
    {
        myfile2 = new Attachment();
        return myfile2;
    }
.....
if(myfile.Id == Null && myfile.Name != null){
                    myfile.ParentId = this.newMember.Id;
                insert myfile;
                }else{
                    Return Null;
                    }
                if(myfile2.Id == Null && myfile2.Name != null){
                    myfile2.ParentId = this.newMember.Id;
                insert myfile2;
                }else{
                    Return Null;
                    } 
SubratSubrat (Salesforce Developers) 
Hello David ,

Here's an updated code snippet that should work for your use case:
 
public SWWRegister_Controller(ApexPages.StandardController controller) {}
public Attachment myfile;
public Attachment myfile2;

public void save() {
if(myfile != null && myfile.Name != null){
myfile.ParentId = this.newMember.Id;
insert myfile;
}
if(myfile2 != null && myfile2.Name != null){
myfile2.ParentId = this.newMember.Id;
insert myfile2;
}
}

In this updated code, I added a new method called save(), which is called when the user clicks the save button on the Visualforce page. In this method, I check if each attachment exists and has a name before inserting it into the Lead record. If an attachment doesn't exist or doesn't have a name, it's skipped, and the code moves on to the next attachment.

If the above code helps , please mark this as Best Answer.
Thank you.
David Marshall 10David Marshall 10
Thanks, Subrat, I appreciate the help and the explanation of the updates, as well.

This worked to allow the document upload to be optional and the rest of the process to be completed whether there were documents uploaded, or not. However, with this updated code, the documents being attached to the Visualforce Page are not getting attached to the Lead record, once submitted. Any thoughts on what may be causing that? For reference, the original snippet I posted was successfully attaching the uploaded documents from the Visualforce Page to the Lead record.