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
Uday KUday K 

How to pass attachment Id from Visualforce page to Controller class

Hi all,

I am showing list of attachments against a record in the visualforce page, also showing a DELETE link  before each attachment. Now i want to delete the particular attachment when clicking on DELETE button. can someone help me out.

<apex:column >
     <apex:facet name="header">Attachment</apex:facet>
           <table border = '0' cellspacing="0" cellpadding="0">
              <tr><td><b><center><apex:outputLink value="/apex/AttachmentUpload?id={!card.time.id}" rendered="{!!(card.time.id ==                                                                                                                null)}">Upload</apex:outputLink></center></b><br/></td>
                        <td><apex:repeat var="attach" value="{!card.time.Attachments}">
                            <apex:outputLink value="/servlet/servlet.FileDownload?file={!attach.id}" target="_blank">
                                {!attach.Name}
                            </apex:outputLink>&nbsp;&nbsp;&nbsp;<b><a href="javascript:if (window.confirm('Are you sure?')) DeleteFile('{!attach.Id}');" style="font-weight:bold">Del</a></b><br/>
                        </apex:repeat></td></tr></table>
                </apex:column>

 Thanks,
Uday
Best Answer chosen by Uday K
Amit ShingaviAmit Shingavi
Hello Uday,

You can use <apex:param> to pass the ID to controller.

VF page :
<apex:outputlink action="{!delAttachment}" href="javascript:if (window.confirm('Are you sure?')) DeleteFile('{!attach.Id}');" style="font-weight:bold">Del
     <apex:param name="contIdParam" value="{!attach.Id}" assignTo="{!IdChoosen}"/>
</apex:outputlink>

Apex Controller:
public String IdChoosen{get; set;}

public PageReference delAttachment()
{
    Attachment AttachmenttoDel=new Attachment(id=contIdChosen);
   delete AttachmenttoDel;
   // Here you can again call Wrapper list method to reload
    return null;
}

You can use the following link for more details :
http://bobbuzzard.blogspot.in/2011/07/passing-parameters-to-apex-method-from.html


Happy Coding!!!

All Answers

Amit ShingaviAmit Shingavi
Hello Uday,

You can use <apex:param> to pass the ID to controller.

VF page :
<apex:outputlink action="{!delAttachment}" href="javascript:if (window.confirm('Are you sure?')) DeleteFile('{!attach.Id}');" style="font-weight:bold">Del
     <apex:param name="contIdParam" value="{!attach.Id}" assignTo="{!IdChoosen}"/>
</apex:outputlink>

Apex Controller:
public String IdChoosen{get; set;}

public PageReference delAttachment()
{
    Attachment AttachmenttoDel=new Attachment(id=contIdChosen);
   delete AttachmenttoDel;
   // Here you can again call Wrapper list method to reload
    return null;
}

You can use the following link for more details :
http://bobbuzzard.blogspot.in/2011/07/passing-parameters-to-apex-method-from.html


Happy Coding!!!

This was selected as the best answer
goabhigogoabhigo
Did you try using < apex:actionSupport > along with <apex:param>? There are good number of examples on this..
goabhigogoabhigo
https://cs11.salesforce.com/apexpages/apexcomponents.apexp
goabhigogoabhigo
Good one Amit. I missed noting <apex:outputLink> in his code.
As  per help and support:
A parameter for the parent component. The < apex:param > component can only be a child of the following components:

    < apex:actionFunction >
    < apex:actionSupport >
    < apex:commandLink >
    < apex:outputLink >
    < apex:outputText >
    < flow:interview >
DevADSDevADS
@Uday : Let me know if you still facing any issues!
dphillippi1.388067067032249E12dphillippi1.388067067032249E12
Alternatively, you can create a wrapper class in your controller for  the attachments and so you can put the delete method directly in there if you do not wish to use the actionFunction or actionSupports:

public class YourController
{
  public List<AttachmentWrapper> wrappers{get; private set;}
  ...

  public void refreshData()
  {
    wrappers = new List<AttachmentWrapper>();
    //Rebuild your wrappers variable here
    ...
    //EX. wrappers.add(new AttachmentWrapper(someAttachment, this));
  }

  public class AttachmentWrapper
  {
    public Attachment attach{get; private set;}
    public YourController controller{private get; private set}

    public AttachmentWrapper(Attachment attach, YourController controller)
    {
      this.attach = attach;
      this.controller = controller;
    }

    public static delAttach()
    {
      delete attach;
      controller.refreshData();
    }
  }
}
BALA_RAMBALA_RAM
VIsualforce Page:
<apex:outputlink action="{!delAttachment}" href="javascript:if (window.confirm('Are you sure?')) DeleteFile('{!attach.Id}');" style="font-weight:bold">Del
     <apex:param name="contIdParam" value="{!attach.Id}" assignTo="{!RowAttachmentId}"/>
</apex:outputlink>

Apex Controller:
public String RowAttachmentId {get; set;}

public PageReference delAttachment()
{
    Attachment DeleteAttach=new Attachment(id=RowAttachmentId);
   delete DeleteAttach;
    return null;
}

above that way is very esay to delete the attachement @ uday K

thanks 
Balu
Uday KUday K
Thanks a lot to everybody. Its done with all your help.