+ Start a Discussion
IbtIbt 

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">

 

<script>
function deleteAttach(attachmentId) {
alert('Entered Javascript With Id' + attachmentId) ;
deleteAttachment(attachmentId);
}
</script>


<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:actionFunction>

-------------

 

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

-----

 

</apex:form>

</apex:page>

 

 

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.

asish1989asish1989

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

http://boards.developerforce.com/t5/Apex-Code-Development/How-do-I-pass-value-of-apex-inputField-to-Custom-Controller/m-p/532931#M96614

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

Thanks

chioikbhchioikbh

Try to add

 

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

 

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.