Wednesday, 8 July 2015

Remote Function with parameter in apex

Class code


public class MyController {

    @RemoteAction
    public static List<Account> getAccountsList(String userId, String value1, String value2) {
 
        return [SELECT Id FROM Account WHERE  id =: userId];
    }
}

Page Script

<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>

No comments:

Post a Comment