0

Apologies in advance for the quality of my code :-)I am not a developer so my skill is limited. I am trying to render a VF page on our case console app which would display all attachments for all emails associated to a case. I thought that I would be able to take an example and tweak it but seems no one else is doing this so I tried from scratch. I need to somehow get the case Id in the controller I tried using the standard controller but not sure if that is correct approach.

Controller:-

Public Class AttachmentController
 {

    public AttachmentController(ApexPages.StandardController controller) {

    }

        Public List<Attachment> GetAttachments(ApexPages.StandardController controller)
        {
          string casId = controller.getRecord().Id;
          List<Attachment> attachedFiles = [SELECT Name, Body, ContentType, parentId FROM Attachment WHERE ParentId IN (SELECT Id FROM EmailMessage WHERE ParentId =:casId)];
          if( attachedFiles != null)
           {
             return attachedFiles;
           }
        return attachedFiles;
        }

  }

VF:-

<apex:page Controller="AttachmentController">


<apex:repeat var="att" value="{!attachedFiles}">

    <apex:outputLink value="/{!att.Id}" target="_blank">View File</apex:outputLink>
</apex:repeat>


</apex:page>

Any help would be greatly appreciated

1

There are some problems with your code.

You need a controller extension. With this, you'll be able to get the current case by using the standard controller parameter in the extension's constructor.

Also, if you want to share a variable between a Visualforce page and a controller, you need a property with a getter and setter or a public method with the name: "Get" + "PropertyName".

Finally, you're not requesting the Id field of the attachments but you're displaying it in the VF Page.

Here's the right code for what you need:

VF Page

<apex:page standardController="Case" extension="AttachmentController">


<apex:repeat var="att" value="{!attachedFiles}">

    <apex:outputLink value="/{!att.Id}" target="_blank">View File</apex:outputLink>
</apex:repeat>
</apex:page>

Extension

Public Class AttachmentController
 {
    public List<Attachment> AttachedFiles {get; set;} //this is a way to share data between the controller and the page

    public AttachmentController(ApexPages.StandardController controller) { //this is where you can get the current case
       this.AttachedFiles = [SELECT Id FROM Attachment WHERE ParentId IN (SELECT Id FROM EmailMessage WHERE ParentId =:controller.getId()];
    }
  }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.