You need to sign in to do that
Don't have an account?
David 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;
}
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;
}
Here's an updated code snippet that should work for your use case:
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.
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.