Find your content:

Search form

You are here

Passing variables from Controller to Page using @RemoteAction

 
Share

I'm using @remoteAction in my controller. My question is whether I'll be able to pass the controller variable values from the remote method to the page?

I tried using static variables in my controller but I don't get the values when the remote method is called.

Is @remoteAction method called asynchronously? But when I check the debug log, I see the remote is getting called after the constructor where I populate the varibles.

Edit :I get the list of accounts I populate in constructor. But when I call the remote method, all I get is an empty list.

function getList(callback) {
$j('#mylist').empty();
MyController.getAccountsList(function(records, e) { showList(records, callback}, {escape:true}); 
return true;
}    

function showList(records, callback) {
console.log(records.length);
}
//remote method in controller
@RemoteAction
global static List<Account> getAccountsList() {
     System.debug(MController.AccountList);
     List<Account> r = MController.AccountList;
     return r;
}

Attribution to: Vignesh Damodharan

Possible Suggestion/Solution #1

You can return the value from Remote action and in the page, you can assign it to the elements in the page.

function assignvalue(ValueOfOrg)
{
    var selectedvalue = document.getElementById("{!$Component.thepage.theform.theblock.thesection.list}").value; 
    MyController.RemoteActionName(selectedvalue, function(result, event)
    {
        if (event.status) 
        {      
            document.getElementById("{!$Component.thepage.theform.theblock.thesection.GetValueFromController}").value = (result.ReturnedValue == null ? '' : result.ReturnedValue); 
        } 
    }, {escape:false});
}

In the above code, I have called RemoteAction "RemoteActionName" with the input parameter "selectedvalue".

The Remote action will return a instance of a inner class(if you use inner class, you can return more than one value from controller). The inner class instance will be saved to 'result'. So you can assign the value from the controller to the page using the below code

document.getElementById("{!$Component.thepage.theform.theblock.thesection.GetValueFromController}").value = (result.ReturnedValue == null ? '' : result.ReturnedValue); 

here, ReturnedValue is the property of my inner class.


Attribution to: Priyanka

Possible Suggestion/Solution #2

Edit:

Having clarified what you are trying to do, an action function is exactly what you are looking for. here is a sample constructor and some sample javascript (taken from this page) for invoking a remote action correctly

public class MyController {

    public String someUsefulValue { get; set; }

    public MyController() {
    }

    @RemoteAction
    public static List<Account> getAccountsList(String userId, String value1, String value2) {
    //Nothing in the controller is accesible from this method, pass everything you need as an argument
        return [SELECT Id FROM Account /*some logic for getting the list, WHERE LIMIT OFFSET etc*/];
    }
}

.

<script type="text/javascript">
    var valueTakenFromSomewhere = 'This will be value1 in the remote action';
    var valueTakenFromSomewhereElse = 'This will be value2 in the remote action';

    Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.MyController.getAccountsList}', 
        '{!$User.Id}',
        valueTakenFromSomewhere,
        valueTakenFromSomewhereElse,
        'This will be value2 in the remote action',
        function(result, event) {
            if (event.status) {
                //do whatever rendering of the list you want
            } else if (event.type === 'exception') {
                //handle errors
            } else {
                //handle unknown responses
            }
        }, 
        {escape: true}
    );
</script>

Attribution to: Daniel Blackhall
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/2114

My Block Status

My Block Content