Hi SalesForce rookie here! I've looked all evening and cannot find a question that seems to cover my problem so I hoped to ask.
I wish to upload a file using VisualForce, a .txt file. I want to read this file's body before it gets stored in a folder.
The code from SalesForce developer provided (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_inputFile.htm) seems to do some of the footwork but I'm not sure how to proceed.
<!-- Page: -->
<apex:page standardController="Document" extensions="documentExt">
<apex:messages />
<apex:form id="theForm">
<apex:pageBlock>
<apex:pageBlockSection>
<apex:inputFile value="{!document.body}" filename="{!document.name}"/>
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
/* Controller */
public class documentExt {
public documentExt(ApexPages.StandardController controller) {
Document d = (Document) controller.getRecord();
d.folderid = UserInfo.getUserId();
}
}
Firstly I cannot overcome automatically routing to the page for the added file. I.E. I wish to remain on the page if possible.
Secondly I cannot get the values to return any information (so as to be able to single out the body for use)
System.debug('document name ' + d.name);
This returns null, even when positioned after Document d = (Document) controller.getRecord();
Finally I cannot figure out how to send the file to the correct library, which I thought would be enabled by the below code
Folder folder = [select id from Folder where name='Financial' LIMIT 1];
d.folderId = folder.id;
But I get the error of "List has no rows for assignment to SObject
An unexpected error has occurred. Your development organization has been notified."
I'm sure what I'm doing is wrong in a simple way but I'm assuming that I'm just too much of rookie to realise! I'm also open to using Apex Triggers or Lightning if they have more sensible solutions.
Best Answer
Returning
null
from yoursave
method will leave you on the same page.The
Document
instance that the file upload will populate is the standard controller one:The error querying the
Folder
objects suggests that a folder of the name you specify doesn't exist. To detect that, assign the query results to a list so you can check its size: