I want to pass a parameter from my VF page into a method in controller.
VF
<div id="output_table">
<apex:form >
<div onclick="Attach.showDeleteConfirmDialog('deleteConfirmDialog{!attach.id}')" class="del_link"></div>
<div id="deleteConfirmDialog{!attach.id}" style="display: none;"
title="Deleting {!attach.name}">
<apex:outputText value="Are you sure?" />
</div>
</apex:form>
<apex:form >
<apex:actionFunction name="doActualDeleteAttachment" action="{!deleteAttachment}" rerender="output_table"></apex:actionFunction>
<apex:param name="attachmentToDeleteParam" value="00Pd000000AcDf1" assignTo="{!selectedAttachmentId}" />
</apex:form>
</div>
So I've hardcoded the value for test purposes.
Controller
public String selectedAttachmentId {get; set;}
public PageReference deleteAttachment() {
Attachment attachment = getSelectedAttachment();
delete attachment;
return null;
}
private Attachment getSelectedAttachment() {
return [SELECT Id FROM Attachment WHERE Id=:selectedAttachmentId];
}
If I simply hardcode an Id into getSelectedAttachment(), it works fine so as far as I can tell, the problem is with the param not being passed into the controller.
<script>
var Attach = (function() {
function showDeleteConfirmDialog(id) {
jQuery('#' + id).dialog({
resizable : false,
modal : true,
buttons : {
'Yes' : function() {
jQuery(this).dialog('close');
doActualDeleteAttachment(id);
return true;
},
'No' : function() {
jQuery(this).dialog('close');
return false;
}
}
});
};
return {
showDeleteConfirmDialog : showDeleteConfirmDialog
};
}());
</script>
Would anyone have any suggestions?
SUCCESS!!
After messing with the code for a while, I realised I was passing
deleteConfirmDialog{!attach.id}
into the controller, not just
{!attach.id}
So the following code now works!
<div onclick="Attach.showDeleteConfirmDialog('deleteConfirmDialog{!attach.id}', '{!attach.Id}')" class="del_link"></div> <--EXTRA ID CALL
and
<script>
var Attach = (function() {
function showDeleteConfirmDialog(id, attachId) <--EXTRA ID VARIABLE{
jQuery('#' + id).dialog({
resizable : false,
modal : true,
buttons : {
'Yes' : function() {
jQuery(this).dialog('close');
doActualDeleteAttachment(Id); <--INCORRECT ID
doActualDeleteAttachment(attachId); <--CORRECT ID
return true;
},
'No' : function() {
jQuery(this).dialog('close');
return false;
}
}
});
};
return {
showDeleteConfirmDialog : showDeleteConfirmDialog
};
}());
</script>
Best Answer
The problem with your code is that the
apex:param
is outside of theapex:actionFunction
. Other code looks ok, but it is uclear what changes you have made on it. So i will post here a working example of passing parameter. Try to modify your code based on this template:Controller:
Visualforce Page:
Debug output: