Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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>
share|improve this question
    
You need to post a visualforce code where you call the actionFunction. I think a problem is there. –  mast0r Apr 17 at 10:11
    
I added it there. I hope it helps! –  Daft Apr 17 at 10:13
    
Try to use only one apex:form tag (best practice using forms) and you have to leave the value parameter of the apex:param empty while passing values, like this value="" –  mast0r Apr 17 at 10:19
    
I've made those changes mast0r but still no luck. 'List has no rows for assignment to SObject' –  Daft Apr 17 at 10:26
add comment

3 Answers 3

up vote 2 down vote accepted

The problem with your code is that the apex:param is outside of the apex: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:

public with sharing class MyController {
    public String passedParam { get; set; }
    public MyController(){
        // It is important to initialize a string here
        passedParam = '';
    }

    // This method just outputs the passed value
    public void checkParam(){
        System.debug('### passedParam: ' + passedParam);
    }
}

Visualforce Page:

<apex:page controller="test2">

    <!-- Only one form tag for all components -->
    <apex:form>

        <apex:actionFunction name="sendParam" action="{!checkParam}" reRender="none">
            <apex:param name="p1" assignTo="{!passedParam}" value=""/>
        </apex:actionFunction>

        <a href="#" onclick="sendParam('someString')">Send it</a>

    </apex:form>

</apex:page>

Debug output:

USER_DEBUG|[43]|DEBUG|### passedParam: someString
share|improve this answer
    
So after solving my first problem, it never would have worked as my param was outside the actionFunction anyway. Cheers for the formatting advice too. –  Daft Apr 17 at 11:23
add comment

use actionfucntion like below because in your code param is outside from the action function which is not the right way. Please give it a try and let us know.

<apex:actionFunction name="doActualDeleteAttachment" action="{!deleteAttachment}" rerender="output_table">
        <apex:param name="attachmentToDeleteParam" value="" assignTo="{!selectedAttachmentId}" />  

</apex:actionFunction>
share|improve this answer
add comment
<apex:param name="attachmentToDeleteParam" value="" assignTo="{!selectedAttachmentId}" />

Change your param code to the above to set its value to id which you are sending from your javascript code.

share|improve this answer
    
Please be more clear while answering questions. At the moment your answer have no details and it is very unclear what is your solution. –  mast0r Apr 17 at 10:26
    
I think Jaffer means I should set my param value to 'value="id"', which is the variable I have declared n my JS. I have already tried that and it didn't work. Thanks anyway Jaffer –  Daft Apr 17 at 10:28
    
Yeh that code was missed. I have posted my new answer. –  Jaffer Ali Apr 17 at 10:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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