1

kindly let me know how to display records using @remoteAction on vf page. kindly rectify my vf page code.currently its displaying null values. Thanks.

apex class:-

public class javascriptremoting {
   @remoteAction
    public static List<Account> loadaccs(){

        return [select Id,Name from Account];
    }
}

vf page:

<apex:page controller="javascriptremoting" id="pg">
<div id="ot" >
   {!names}
</div>
<script>
javascriptremoting.loadaccs(function(result,event){
var names=[];
//var names='';
for(integer i=0;i<=result.length;i++){
names=result[i].Name;
}
document.getElementById('ot').innerHtml=names;

});
</script>
</apex:page>
0

2 Answers 2

3

Here you go: Tried this in my org and it works. Remember this is Javascript so you cannot use the "Integer" keyword in the for loop. Also fixed up the looping mechanism.

Please use better naming conventions. Also review this great answer here on understanding JS remoting. Get List Values in JavaScript Remoting

You are now working on a list of accounts returned and not a single object. Try this.

<apex:page controller="javascriptremoting" id="pg">
<div id="ot" >
</div>
<script>
Visualforce.remoting.Manager.invokeAction(
    '{!$RemoteAction.javascriptremoting.loadaccs}',
    function(result, event){
        var names = [];
        var keyName ="Id";
        if (event.status) {
            console.log (result);
            for(keyName in result){
             if( result.hasOwnProperty(keyName ) ) {     

                 names += result[keyName].Name + '<br/>';

                 }
            }
            document.getElementById('ot').innerHTML = names;
        }
    }, 
    {escape: true}
);
</script>
</apex:page>
0
0

The problem probably is in you function. The correct syntax for JavaScript Remoting is found here:

Visualforce.remoting.Manager.invokeAction(
    '{!$RemoteAction.javascriptremoting.loadaccs}',
    function(result, event){
        var names = [];
        if (event.status) {
            for(i=0;i<=result.length;i++){
                 names = result[i].Name;
            }
            document.getElementById('ot').innerHTML = names;
        }
    }, 
    {escape: true}
);

Also, make sure that your org has accounts and that the user has access to them.


And, since you're just trying to query some accounts, please consider using Remote Objects

0

You must log in to answer this question.

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