3

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;
}
3
  • Can you post some code to illustrate the problem?
    – metadaddy
    Oct 10, 2012 at 4:44
  • Do you want to pass a single variable value or more than one ?
    – Priyanka
    Oct 10, 2012 at 4:51
  • @metadaddy : I've added the code for illustration Oct 10, 2012 at 6:15

2 Answers 2

3

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>
4
  • Accepted! But, I need to use the recordslist in the page as a javascript array to populate it dynamically using jQuery. Thats why I thought i'll use @remoteaction. Oct 10, 2012 at 6:24
  • my answer is for passing data back to a controller, you are asking for passing data to a visualforce page. RemoteAction is the right way to go. Are you clear about the difference between static and instance methods? I think you might be populating your AccountList in the controller constructor and expecting it to be available to the static remote action. Oct 10, 2012 at 6:39
  • Yes. I'm not very clear about them. I think, if I use as static, there'll be only one list across all the users having the same list. But I want it work like the instance variables though where I'll have different list for users based on their ids(if I use the condition in querying). I've thought of a solution but not sure if it'll be the proper way. Oct 10, 2012 at 6:48
  • My proposed solution is using the action function in page and get the controller variables using merge fields which are in a Output panel and re-render the panel on action function call. Inside the panel, write a script to convert it to a javascript array. Is this a good design? Oct 10, 2012 at 6:51
2

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.

1
  • I do use inner class but will the values will be retained in the controller variables? As in, my list is getting populated in the constructor and the list is static but, when I access in the remote call, the list is empty. Oct 10, 2012 at 6:18

You must log in to answer this question.

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