You need to sign in to do that
Don't have an account?
The result from apex class cannot be accessed - JS RemoteAction
I am trying out JS RemoteAction with the help of Apex dev guide.
I am trying to return the result of account , but the result.id or result.name is coming as null whereas result is not null. What is the reason? Please help..
VF code
<apex:page controller="VFRemoteAction" sidebar="false">
<head>
<style>
#errors{color:red;font-size:15px;font-style:bold;margin:5px; width:100%;}
</style>
<script>
var getAccount = function(){
var accountName = document.getElementById('account_name').value;
Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.VFRemoteAction.getAccount}',
accountName,
function(result,event){
if(event.status){
document.getElementById('result').innerHTML=result.id; --> null ????????
}
else{
document.getElementById('errors').innerHTML="Error --> "+event.message;
}
},
{escape: true}
);
}
</script>
</head>
<body>
<apex:pageBlock title="Javascript Remote Sample" >
<apex:pageBlockSection title="Search for an account" columns="2">
<div id="errors">
</div>
<div id="input">
<label>Enter the account name:</label>
<input type="text" name="account_name" id="account_name"/>
<button onclick="getAccount()">Get Account</button>
</div>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:outputPanel >
<div id="result">
</div>
</apex:outputPanel>
</body>
</apex:page>
Apex code
global with sharing class VFRemoteAction {
public static Account account{get;set;}
public String accountName{get;set;}
public VFRemoteAction(){
}
@RemoteAction
global static Account getAccount(String accountName){
account = [SELECT id,name ,site FROM ACCOUNT where name=:accountName];
return account;
}
}
I am trying to return the result of account , but the result.id or result.name is coming as null whereas result is not null. What is the reason? Please help..
VF code
<apex:page controller="VFRemoteAction" sidebar="false">
<head>
<style>
#errors{color:red;font-size:15px;font-style:bold;margin:5px; width:100%;}
</style>
<script>
var getAccount = function(){
var accountName = document.getElementById('account_name').value;
Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.VFRemoteAction.getAccount}',
accountName,
function(result,event){
if(event.status){
document.getElementById('result').innerHTML=result.id; --> null ????????
}
else{
document.getElementById('errors').innerHTML="Error --> "+event.message;
}
},
{escape: true}
);
}
</script>
</head>
<body>
<apex:pageBlock title="Javascript Remote Sample" >
<apex:pageBlockSection title="Search for an account" columns="2">
<div id="errors">
</div>
<div id="input">
<label>Enter the account name:</label>
<input type="text" name="account_name" id="account_name"/>
<button onclick="getAccount()">Get Account</button>
</div>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:outputPanel >
<div id="result">
</div>
</apex:outputPanel>
</body>
</apex:page>
Apex code
global with sharing class VFRemoteAction {
public static Account account{get;set;}
public String accountName{get;set;}
public VFRemoteAction(){
}
@RemoteAction
global static Account getAccount(String accountName){
account = [SELECT id,name ,site FROM ACCOUNT where name=:accountName];
return account;
}
}
JS is casesensitive and APEX is not.
Use:
It should work.
All Answers
JS is casesensitive and APEX is not.
Use:
It should work.