Find your content:

Search form

You are here

Passing variable into actionFunction and then back to JS function oncomplete

 
Share

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.


Attribution to: Daft

Possible Suggestion/Solution #1

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

Attribution to: abd3721

Possible Suggestion/Solution #2

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>

Attribution to: Daft
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33827

My Block Status

My Block Content