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
Benjamin BolopueBenjamin Bolopue 

Extract Attachment ID and use it in Field Update

My requirements are to
  • 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
So far, I've figured out everything but the extraction of the Attachment's ID. Here's what I have so far. Can anybody help me add the last piece of the puzzle (extracting the Attachment's ID and inserting it into 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

 
Best Answer chosen by Benjamin Bolopue
Abhishek BansalAbhishek Bansal
Hi Benjamin,

I have updated your code with the comments as "Added by Abhishek".
Please use the below upodated controller class :
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;
		
		//Added by Abhishek Bansal : Start
		pageCase.CasePhoto1ID__c = [Select Id from Attachment where Name =:fixedFileName AND parentId =: pageCase.id].id;
		update pageCase;
		//Added by Abhishek Bansal : End
        
        return null;
    }
  }


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

Daniel B ProbertDaniel B Probert
add something like this around line 39:
 
Case updatecase = new Case(id=pageCase.Id);
            updatecase.CasePhoto1ID__c = a.id;
update updatecase;

 
Abhishek BansalAbhishek Bansal
Hi Benjamin,

I have updated your code with the comments as "Added by Abhishek".
Please use the below upodated controller class :
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;
		
		//Added by Abhishek Bansal : Start
		pageCase.CasePhoto1ID__c = [Select Id from Attachment where Name =:fixedFileName AND parentId =: pageCase.id].id;
		update pageCase;
		//Added by Abhishek Bansal : End
        
        return null;
    }
  }


Let me know if you have any problem with the above code or if you want any further help on this.

Thanks,
Abhishek Bansal.

This was selected as the best answer
Benjamin BolopueBenjamin Bolopue
Thank you both for your responses!

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
Abhishek BansalAbhishek Bansal

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.

Benjamin BolopueBenjamin Bolopue
THIS is why people like me need people like you!

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
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;
        
       //Added by Abhishek Bansal : Start
        pageCase.IGS_8D_Photo_1_ID__c = [Select Id from Attachment where Name =:fixedFileName AND parentId =: pageCase.id].id;
        update pageCase;
        //Added by Abhishek Bansal : End
        
        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="{!casePicFile}" />
            <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>

 
Benjamin BolopueBenjamin Bolopue
ARG! Nevermind. The Controller & VF page is working exactly as intended! The ID, however, isn't populating until the record is refreshed. Is there something I can add to the VF page/controller to make the page auto-refresh after the ID field has been updated?
Abhishek BansalAbhishek Bansal

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.

Benjamin BolopueBenjamin Bolopue
Hi Abhishek,
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