You need to sign in to do that
Don't have an account?

Javascript remoting to display a list of records in a table format
“Have been trying to learn Javascript Remoting since hours. I have manipulated this example to display a table of records with remoting an apex class. Following is the controller and VF.. My controller pulls all records from custom object Feedback. But i'm unable to display in VF. Please guide.
global with sharing class AccountRemoter {
public String accountName { get; set; }
public Feedback__c account { get; set; }
public AccountRemoter() { }
@RemoteAction
global static List<Feedback__c> getAccount(String accountName) {
List<Feedback__c> account = [SELECT Name FROM Feedback__c ];
return account;
}
}
and the Visualforce page...
<apex:page controller="AccountRemoter">
<script type="text/javascript">
function getRemoteAccount() {
var accountName = document.getElementById('acctSearch').value;
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.AccountRemoter.getAccount}',
accountName,
function(result, event){
if (event.status) {
// Get DOM IDs for HTML and Visualforce elements like this
document.getElementById('remoteAcctId').innerHTML = result.Id
document.getElementById(
"{!$Component.block.blockSection.secondItem.acctNumEmployees}"
).innerHTML = result.NumberOfEmployees;
} else if (event.type === 'exception') {
document.getElementById("responseErrors").innerHTML =
event.message + "<br/>\n<pre>" + event.where + "</pre>";
} else {
document.getElementById("responseErrors").innerHTML = event.message;
}
},
{escape: true}
);
}
</script>
<input id="acctSearch" type="text"/>
<button onclick="getRemoteAccount()">Get Feedbacks</button>
<div id="responseErrors"></div>
<apex:pageBlock id="block">
<apex:pageBlockSection id="blockSection" columns="2">
<apex:pageBlockSectionItem id="firstItem">
<span id="remoteAcctId"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem id="secondItem">
<apex:outputText id="acctNumEmployees"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
The closest to a table on click of the "Get Feedback" button, I have achieved so far with undefined results.... enter image description here
Any help will be appreciated. Thanks
UPDATE: Fields in Feedback
Client_Feedback_App__c
Description__c
Importance__c
Resolution__c
Status__c
Summary__c
global with sharing class AccountRemoter {
public String accountName { get; set; }
public Feedback__c account { get; set; }
public AccountRemoter() { }
@RemoteAction
global static List<Feedback__c> getAccount(String accountName) {
List<Feedback__c> account = [SELECT Name FROM Feedback__c ];
return account;
}
}
and the Visualforce page...
<apex:page controller="AccountRemoter">
<script type="text/javascript">
function getRemoteAccount() {
var accountName = document.getElementById('acctSearch').value;
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.AccountRemoter.getAccount}',
accountName,
function(result, event){
if (event.status) {
// Get DOM IDs for HTML and Visualforce elements like this
document.getElementById('remoteAcctId').innerHTML = result.Id
document.getElementById(
"{!$Component.block.blockSection.secondItem.acctNumEmployees}"
).innerHTML = result.NumberOfEmployees;
} else if (event.type === 'exception') {
document.getElementById("responseErrors").innerHTML =
event.message + "<br/>\n<pre>" + event.where + "</pre>";
} else {
document.getElementById("responseErrors").innerHTML = event.message;
}
},
{escape: true}
);
}
</script>
<input id="acctSearch" type="text"/>
<button onclick="getRemoteAccount()">Get Feedbacks</button>
<div id="responseErrors"></div>
<apex:pageBlock id="block">
<apex:pageBlockSection id="blockSection" columns="2">
<apex:pageBlockSectionItem id="firstItem">
<span id="remoteAcctId"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem id="secondItem">
<apex:outputText id="acctNumEmployees"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
The closest to a table on click of the "Get Feedback" button, I have achieved so far with undefined results.... enter image description here
Any help will be appreciated. Thanks
UPDATE: Fields in Feedback
Client_Feedback_App__c
Description__c
Importance__c
Resolution__c
Status__c
Summary__c
And then to update, you have to option:
<apex:actionFunction name="actionFunctionChangeRemoteAcctId" action="{!apexMethodChangeRemoteAcctId}" rerender="">
<input id="acctSearch" type="text"/>
<button onclick="getRemoteAccount()">Get Feedbacks</button>
<div id="responseErrors"></div>
<apex:pageBlock id="block">
</apex:page>
APEX CODE:
public void apexMethodChangeRemoteAcctId(){
accountName = vRemoteAcctId;
account.name = accountName;
update account;
<script type="text/javascript">
var jsVarNumerEmployees = document.getElementById( "{!$Component.block.blockSection.secondItem.acctNumEmployees}" ).value;
actionFunctionToUploadAll(jsVarRemoteAcctId,jsVarNumerEmployees);
<apex:actionFunction name="actionFunctionToUploadAll" action="{!apexMethodToUploadAll}" rerender="">
<apex:param name="paramNumberEmployees" value=""/>
<input id="acctSearch" type="text"/>
<button onclick="getRemoteAccount()">Get Feedbacks</button>
<div id="responseErrors"></div>
<apex:pageBlockSectionItem id="secondItem">
<apex:pageBlockSectionItem id="secondItem">
APEX CODE:
public void apexMethodToUploadAll(){
String vNumberEmployees = Apexpages.currentPage().getParameters().get('paramNumberEmployees');
account.name = accountName;
account.NumEmployees = vNumberEmployees; //Put the real field API name
update account;
Hopefully this answer your question.
Pablo.
All Answers
Any help will be appreciated. Please reply here :)
APEX CODE:
global with sharing class AccountRemoter {
public String accountName { get; set; }
public Feedback__c account { get; set; }
public AccountRemoter() { }
@RemoteAction
global static List<Feedback__c> mGetAccount(String accountName) {
List<Feedback__c> account = [SELECT Id, Name FROM Feedback__c ];
return account;
}
}
VFP CODE:
<apex:page controller="AccountRemoter">
<script type="text/javascript">
function getRemoteAccount() {
var accountName = document.getElementById('acctSearch').value;
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.AccountRemoter.mGetAccount}',
accountName,
function(result, event){
if (event.status) {
// Get DOM IDs for HTML and Visualforce elements like this
for (var i = 0; i < result.length; i++) {
document.getElementById('remoteAcctId').innerHTML = result[i].Id;
document.getElementById( "{!$Component.block.blockSection.secondItem.acctNumEmployees}" ).innerHTML = result[i].name;
}
.............
}else if (event.type === 'exception') {
document.getElementById("responseErrors").innerHTML =
event.message + "<br/>\n<pre>" + event.where + "</pre>";
} else {
document.getElementById("responseErrors").innerHTML = event.message;
}
}, {escape: true});
}
</script>
<input id="acctSearch" type="text"/>
<button onclick="getRemoteAccount()">Get Feedbacks</button>
<div id="responseErrors"></div>
<apex:pageBlock id="block">
<apex:pageBlockSection id="blockSection" columns="2">
<apex:pageBlockSectionItem id="firstItem">
<span id="remoteAcctId"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem id="secondItem">
<apex:outputText id="acctNumEmployees"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
If I try alert(JSON.stringify(result)); then I get all the records, which tell I have all the records, But unable to display it properly. Can you suggest what to do ?
Where you had -> document.getElementById('remoteAcctId').innerHTML = result[i].Id;
document.getElementById( "{!$Component.block.blockSection.secondItem.acctNumEmployees}" ).innerHTML = result[i].name;
Write this one -> document.getElementById('remoteAcctId').innerHTML += result[i].Id;
document.getElementById( "{!$Component.block.blockSection.secondItem.acctNumEmployees}" ).innerHTML += result[i].name;
I got all the results for these changes...
document.getElementById('remoteAcctId').innerHTML += result[i].Name+ "<br/>";
document.getElementById( "{!$Component.block.blockSection.secondItem.acctNumEmployees}" ).innerHTML += result[i].name+"<br/>";
one column as name of the records and other column as UNDEFINED :(
document.getElementById('remoteAcctId').innerHTML += resultName+ '<br/>';
document.getElementById( "{!$Component.block.blockSection.secondItem.acctNumEmployees}" ).innerHTML += resultName+'<br/>';
And then to update, you have to option:
<apex:actionFunction name="actionFunctionChangeRemoteAcctId" action="{!apexMethodChangeRemoteAcctId}" rerender="">
<input id="acctSearch" type="text"/>
<button onclick="getRemoteAccount()">Get Feedbacks</button>
<div id="responseErrors"></div>
<apex:pageBlock id="block">
</apex:page>
APEX CODE:
public void apexMethodChangeRemoteAcctId(){
accountName = vRemoteAcctId;
account.name = accountName;
update account;
<script type="text/javascript">
var jsVarNumerEmployees = document.getElementById( "{!$Component.block.blockSection.secondItem.acctNumEmployees}" ).value;
actionFunctionToUploadAll(jsVarRemoteAcctId,jsVarNumerEmployees);
<apex:actionFunction name="actionFunctionToUploadAll" action="{!apexMethodToUploadAll}" rerender="">
<apex:param name="paramNumberEmployees" value=""/>
<input id="acctSearch" type="text"/>
<button onclick="getRemoteAccount()">Get Feedbacks</button>
<div id="responseErrors"></div>
<apex:pageBlockSectionItem id="secondItem">
<apex:pageBlockSectionItem id="secondItem">
APEX CODE:
public void apexMethodToUploadAll(){
String vNumberEmployees = Apexpages.currentPage().getParameters().get('paramNumberEmployees');
account.name = accountName;
account.NumEmployees = vNumberEmployees; //Put the real field API name
update account;
Hopefully this answer your question.
Pablo.
Thanks,
Pablo.
<!--
Author: Swapnil Pal
Date: 14th September 2017
Purpose: Display the list of account using remote action
-->
<apex:page controller="AccountRemoteCall">
<script type="text/javascript">
//This function retrives the information of the remote call and store them in table
function getRemoteAccount() {
//Making Remote call to fetch account information
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.AccountRemoteCall.getAccount}',
function(result, event){
if (event.status) {
console.log(result.length);
// Creating a table to retrive data
var accountData = "<table border='1|1'>";
for (var i = 0; i < result.length; i++) {
console.log(result[i].Name);
accountData+="<tr>";
accountData+="<td>"+result[i].Name+"</td>";
accountData+="<td>"+result[i].AccountNumber+"</td>";
accountData+="<td>"+result[i].NumberOfEmployees+"</td>";
accountData+="</tr>";}
accountData+="</table>";
document.getElementById("display").innerHTML = accountData;
} else if (event.type === 'exception') {
document.getElementById("responseErrors").innerHTML =
event.message + "<br/>\n<pre>" + event.where + "</pre>";
} else {
document.getElementById("responseErrors").innerHTML = event.message;
}
},
{escape: true}
);
}
window.onload = getRemoteAccount;
</script>
<div id="responseErrors"></div>
<div id="display"></div>
</apex:page>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Controller:
global with sharing class AccountRemoteCall {
public static List<Account> account { get; set; }
public AccountRemoteCall() { } // empty constructor
//Fetching the information of the list of Account
@RemoteAction
global static List<Account> getAccount() {
account = [SELECT Name, AccountNumber, AnnualRevenue, NumberOfEmployees FROM Account];
System.debug(account);
return account;
}
}