You need to sign in to do that
Don't have an account?
Extract Attachment ID and use it in Field Update
My requirements are to
Here's the Controller:
VF Page
Any help would be much appreciated!
Thanks,
Ben
- Incorporate a VF Page into the Case Object where Users could upload a single image
- Have that single image stored as an Attachment for that Case
- Renaming that Attachment 'CasePhoto1.jpg'
- Extract that Attachment's ID and insert it into a custom field named CasePhoto1ID__c
Here's the Controller:
public with sharing class CasePhotoUploadController1 { Private Static FINAL String fixedFileName = 'CasePhoto1.jpg'; public boolean displaying { get; set; } public Case pageCase; public Blob casePicFile { get; set; } public Id currentPicture { get; set; } /** Constructor, grab record, and check/load an existing photo */ public CasePhotoUploadController1(ApexPages.StandardController controller) { pageCase = (Case)controller.getRecord(); List<attachment> currentPictures = [SELECT Id FROM Attachment WHERE parentId = :pageCase.Id AND name = :fixedFileName LIMIT 1]; if(currentPictures.size() != 0) { currentPicture = currentPictures.get(0).Id; } displaying = true; } /** toggle switches between the photo display and photo upload form */ public void toggle() { displaying = !displaying; } /** saveFile clears any existing (secondary) Case picture, retrieves the data from the form, and saves it under the relevant filename*/ Public Pagereference saveFile() { // first, we cannot have any conflicting files List<attachment> savedPicture = [SELECT Id, name, body FROM Attachment WHERE parentId = :pageCase.Id AND name = :fixedFileName]; if(savedPicture.size() > 0) { delete savedPicture; } // Now, the new blob is saved Attachment a = new Attachment(parentId = pageCase.Id, name = fixedFileName, body = casePicFile); insert a; currentPicture = a.Id; displaying = true; return null; } }
VF Page
<apex:page standardController="Case" extensions="CasePhotoUploadController1" showHeader="false" standardStyleSheets="true" sidebar="false"> <apex:form id="contentForm"> <div style="height:170px;"> <apex:pageBlock mode="edit"> <apex:pageblocksection columns="1" rendered="{!displaying}"> <apex:image height="150" value="{!URLFOR($Action.Attachment.Download, currentPicture)}" rendered="{!currentPicture != null}"/> <apex:outputPanel rendered="{!currentPicture == null}"><em>No picture currently available</em></apex:outputPanel> </apex:pageblocksection> <apex:pageblocksection columns="1" rendered="{! !displaying}"> <p>Use the button to below to select a new file and then press "Save"</p> <apex:inputFile value="{!profilePicFile}" /> <p>Or press Cancel to return.</p> </apex:pageBlockSection> </apex:pageBlock> </div> <apex:commandButton value="Upload new photo" action="{!toggle}" rerender="contentForm" rendered="{!displaying && currentPicture!=null}"/> <apex:commandButton value="Upload photo" action="{!toggle}" rerender="contentForm" rendered="{!displaying && currentPicture==null}"/> <apex:commandButton value="Cancel" action="{!toggle}" rendered="{! !displaying}"/> <apex:commandButton value="Save" action="{!saveFile}" rendered="{! !displaying}"/> </apex:form> </apex:page>
Any help would be much appreciated!
Thanks,
Ben
I have updated your code with the comments as "Added by Abhishek".
Please use the below upodated controller class :
Let me know if you have any problem with the above code or if you want any further help on this.
Thanks,
Abhishek Bansal.
All Answers
I have updated your code with the comments as "Added by Abhishek".
Please use the below upodated controller class :
Let me know if you have any problem with the above code or if you want any further help on this.
Thanks,
Abhishek Bansal.
Abhishek,
I'm getting the following error when attempting to save the controller with your additions: "Error: Compile Error: expecting right curly bracket, found '<' at line 44 column 9".
I've tried removing the <i> and </i> tags (leaving your comments in place), but then I get the following error: "Error: Compile Error: The property Blob profilePicFile is referenced by Visualforce Page (Display_Attachments) in salesforce.com. Remove the usage and try again. at line 7 column 17"
Any thoughts?
Daniel,
I'm getting the same "Error: Compile Error: The property Blob profilePicFile is referenced by Visualforce Page (Display_Attachments) in salesforce.com. Remove the usage and try again. at line 7 column 17" when I try inserting the lines you suggested.
Any thoughts?
Again, thank you both for your time and input!
Ben
Hi Ben,
In your VF page you have used a property "profilePicFile" al line no. 14 but i did not find this property being declared in your controller class and this is all about the error that you are facing while saving my code.
Please declare this property in your controller class and than try to save the code.
Note : Please do not include <i> and </i> tag in class.
Let me know if you need more information on this.
Thanks,
Abhishek Bansal.
Alright, I got both the VF page and controller in place without error. That said, the ID field isn't updating.
Please note, I've changed the name of the ID field to "IGS_8D_Photo_1_ID__c" and updated the code as needed (I believe).
Here are the Controller and VF page. Any thoughts?
Thanks
Controller
VF Page
Hi Ben,
Which page you want to refresh ?
If you want to refresh the VF page that you have mentioned above than you can use the "reRender" attribute but if you are talking about the detail page of the Case record than it is not possible.
Please clarify your requirement in detail.
Thanks,
Abhishek Bansal.
I was actually looking to refresh the details page. As you mentioned, this isn't possible. Not without the use of Javascript. I haven't tried it yet, but the solution described here (https://developer.salesforce.com/forums/?id=906F0000000972tIAA" target="_blank)seems to be what I'm in need of. Any reason to think I wouldn't be able to incorporate the same methodology into my existing Controller/VF Page?
Thanks!
Ben