+ Start a Discussion

Passing a parameter to an Apex Function, from javascript

        I designed a visual force page to display list of opportunity attachments. It's displaying opporunities with remove link also. Written onclick event of remove link. Onclick of the link, javascript function is calling and displaying alert of attachment id. Used <apex:actionFunction> for this and defined oncomplete attribute also, in that  alert is given. This alert also displaying onclick of remove link but deleteAttachment() apex method is not calling(It contains logic to delete attachment). Please guide me if any thing wrong in code.


Visual force page:


<apex:page controller="SendController">


function deleteAttach(attachmentId) {
alert('Entered Javascript With Id' + attachmentId) ;

<apex:form enctype="multipart/form-data" id="frm">
<apex:actionFunction name="deleteAttachment" action="{!deleteAttachment}" oncomplete="alert('ok');">
<apex:param name="attachmentId" assignTo="{!attachmentId}" value=""/>



<apex:repeat value="{!attResults}" var="attResult">
<apex:outputLink value="{!URLFOR($Action.Attachment.Download, attResult.id)}" target="_blank">
<apex:outputLink onclick="deleteAttach('{!attResult.id}');" >







Apex class:


public class SendController {  


public List<Attachment> attResults {get; set;}


public SendController() {
System.debug('Inside SendWithSutiSign constructor');

attResults = [select Id, Name from Attachment Where ParentId = :id];         



public String attachmentId {get; set;}
public void deleteAttachment() {
System.debug('Inside deleteAttachment method');
try {
System.debug('Attachment Id1=>' + attachmentId);
System.debug('Attachment Id1=>' + Apexpages.currentPage().getParameters().get('firstParam'));
Attachment attc = [select Id, Name from Attachment Where Id = :attachmentId];
delete attc;
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Attachment deleted successfully'));

//Get attachment list of opportunity
attResults = [select Id, Name from Attachment Where ParentId = :id];
} catch (DMLException e) {
System.debug('Exception in deleteAttachment method');
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error deleteing attachment'));




Thanks in advance.


Please go through this link. I have a similar post regarding  Action functions and parameter passing from java script to controller


If this post answers your question please , mark it as solutions and give kudos if this helps you



Try to add


<apex:actionFunction name="deleteAttachment" action="{!deleteAttachment}" oncomplete="alert('ok'); " rerender="somePanel">
<apex:param name="attachmentId" assignTo="{!attachmentId}" value=""/>


If you don't have any panel to rerender, just make a hidden outputpanel and rerender it. Don't know why, but actionFunction only works when it has rerender attribute by experience.