4

Can I pass a variable into an apex:actionFunction call as follows:

deleteThing(thing_id);

<apex:actionFunction name="deleteThing" action="{!deleteAttachment}" reRender="none"
                     oncomplete="remove_deleted_thing(thing_id)">
    <apex:param name="thingToDelete" value="" assignTo="{!selectedAttachmentId}" />  
</apex:actionFunction>


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];
}

function remove_deleted_thing(thing_id) {
    console.log(thing_id);
}

So that after {!deleteAttachment} runs in my controller, thing_id will be passed to remove_deleted_thing()?

I'm not even getting 'undefined' in my console now, so I assume I'm doing somthing wrong here. The code does what it's supposed to do up until remove_deleted_thing.

Amy suggestions or advice would be greatly appreciated.

4
  • try putting return false; so the code would be oncomplete="remove_deleted_thing(thing_id);return false;"
    – highfive
    Commented Apr 28, 2014 at 10:31
  • thanks for the suggestion highfive but that didn't work.
    – Daft
    Commented Apr 28, 2014 at 10:38
  • Are you sure that your action method working fine without any exceptions?
    – highfive
    Commented Apr 28, 2014 at 10:41
  • Well no errors have popped up yet and it's deleting my records. You can see it's a very simple method. Since it's deleting the record, could the Id be deleted along with it? And therefore can't be passed on to the next function? Does that even make sense??
    – Daft
    Commented Apr 28, 2014 at 10:44

2 Answers 2

4

I changed my actionFunction to the following, and all is well :)

<apex:actionFunction name="deleteThing" action="{!deleteAttachment}" reRender="none" 
                 oncomplete="remove_deleted_thing('{!selectedAttachmentId}') <-- HERES THE CHANGE">
    <apex:param name="thingToDelete" value="" assignTo="{!selectedAttachmentId}" />  
</apex:actionFunction>
0

Although this is a "hack" that could potentially be broken by Salesforce updates you can also write:

<apex:actionFunction name="deleteThing" action="{!deleteAttachment}" reRender="none" 
  oncomplete="remove_deleted_thing(request.options.parameters.thingToDelete)">
<apex:param name="thingToDelete" value="" assignTo="{!selectedAttachmentId}" />  

The onComplete method of the action function in Visualforce appears like this when it's compiled down to JS:

function(request,event,data) {
  remove_deleted_thing(request.options.parameters.thingToDelete);
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .