function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MnZ321MnZ321 

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
Best Answer chosen by MnZ321
Pablo_RoldanPablo_Roldan
Ok, So the first thing it's that you need some input tag to manipulate these fields.
And then to update, you have to option:
  • First one it's to have an onchange event in each input, something like this:
VFP CODE:
<apex:actionFunction name="actionFunctionChangeRemoteAcctId" action="{!apexMethodChangeRemoteAcctId}" rerender="">
<apex:param name="paramRemoteAcctId" value=""/>
</apex:actionFunction>
<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">            
<input type="text" id="remoteAcctId" onchange="actionFunctionChangeRemoteAcctId(this.value);"/>        
</apex:pageBlockSectionItem>        
<apex:pageBlockSectionItem id="secondItem">            
<apex:outputText id="acctNumEmployees"/>        
</apex:pageBlockSectionItem>    
</apex:pageBlockSection>   
</apex:pageBlock>   
</apex:page>



APEX CODE:
public void apexMethodChangeRemoteAcctId(){
String vRemoteAcctId = Apexpages.currentPage().getParameters().get('paramRemoteAcctId');
accountName = vRemoteAcctId;

account.name = accountName;
update account;
}
  • Second one it's using a submit button to get everything and then upload it.
VFP CODE:
<script type="text/javascript">
function jsMethodToUploadAll(){
var jsVarRemoteAcctId = document.getElementById('remoteAcctId').value;
var jsVarNumerEmployees = document.getElementById( "{!$Component.block.blockSection.secondItem.acctNumEmployees}" ).value;
actionFunctionToUploadAll(jsVarRemoteAcctId,jsVarNumerEmployees);
}
</script>
<apex:actionFunction name="actionFunctionToUploadAll" action="{!apexMethodToUploadAll}" rerender="">
<apex:param name="paramRemoteAcctId" value=""/>
<apex:param name="paramNumberEmployees" value=""/>
</apex:actionFunction>
<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">           
<input type="text" id="remoteAcctId"/>       
</apex:pageBlockSectionItem>       
<apex:pageBlockSectionItem id="secondItem">           
<input type="text" id="acctNumEmployees"/>       
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem id="secondItem">          
<input type="button" value="Submit" onclick="jsMethodToUploadAll();"/>      
</apex:pageBlockSectionItem>  
</apex:pageBlockSection>  
</apex:pageBlock>  
</apex:page>


APEX CODE:
public void apexMethodToUploadAll(){
String vRemoteAcctId = Apexpages.currentPage().getParameters().get('paramRemoteAcctId');
String vNumberEmployees = Apexpages.currentPage().getParameters().get('paramNumberEmployees');
accountName = vRemoteAcctId;
account.name = accountName;
account.NumEmployees = vNumberEmployees; //Put the real field API name
update account;
}
 

Hopefully this answer your question.
Pablo.
 
 

All Answers

MnZ321MnZ321
I'm unable to post the code ...please follow this http://salesforce.stackexchange.com/questions/32748/javascript-remoting-to-display-a-table-of-records


Any help will be appreciated. Please reply here :)
Pablo_RoldanPablo_Roldan
try this:
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;               
   }

.............
MnZ321MnZ321
@pablo Is the VF page incomplete? 
Pablo_RoldanPablo_Roldan
Yep, because everthing else from there is exactly the same
Pablo_RoldanPablo_Roldan
MnZ321, this is the code next to the previous one:
            }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>
MnZ321MnZ321
I get only 1 record from the Feedback object. How can I get all the records?
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 ?
Pablo_RoldanPablo_Roldan
Sorry, that was my fault. the code for that is the next:

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;
MnZ321MnZ321
With a little change i did get the results, partially.... 
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 :(
Pablo_RoldanPablo_Roldan
The other column is undefined, because you have already use Name, you could try two things.
  • try with Id
document.getElementById('remoteAcctId').innerHTML += result[i].Id+ '<br/>';
  • try to save name in a variable, and then use this several times.
var resultName = result[i].Name;
document.getElementById('remoteAcctId').innerHTML += resultName+ '<br/>';
                               document.getElementById( "{!$Component.block.blockSection.secondItem.acctNumEmployees}" ).innerHTML += resultName+'<br/>';
MnZ321MnZ321
Yes I'm able to have both the fields now. Can I , by any chance, make the name field editable? Is there any tag?
Pablo_RoldanPablo_Roldan
could you explain more the thing which you want to do?
MnZ321MnZ321
I have an object Feedback which has a name field, which u just helped me to display it on a vf page. So i want to know if i can edit and update the name field from the vf page itself? Will inlineeditsupport work? Or do i have to write a custom controller/extension?
Pablo_RoldanPablo_Roldan
Ok, So the first thing it's that you need some input tag to manipulate these fields.
And then to update, you have to option:
  • First one it's to have an onchange event in each input, something like this:
VFP CODE:
<apex:actionFunction name="actionFunctionChangeRemoteAcctId" action="{!apexMethodChangeRemoteAcctId}" rerender="">
<apex:param name="paramRemoteAcctId" value=""/>
</apex:actionFunction>
<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">            
<input type="text" id="remoteAcctId" onchange="actionFunctionChangeRemoteAcctId(this.value);"/>        
</apex:pageBlockSectionItem>        
<apex:pageBlockSectionItem id="secondItem">            
<apex:outputText id="acctNumEmployees"/>        
</apex:pageBlockSectionItem>    
</apex:pageBlockSection>   
</apex:pageBlock>   
</apex:page>



APEX CODE:
public void apexMethodChangeRemoteAcctId(){
String vRemoteAcctId = Apexpages.currentPage().getParameters().get('paramRemoteAcctId');
accountName = vRemoteAcctId;

account.name = accountName;
update account;
}
  • Second one it's using a submit button to get everything and then upload it.
VFP CODE:
<script type="text/javascript">
function jsMethodToUploadAll(){
var jsVarRemoteAcctId = document.getElementById('remoteAcctId').value;
var jsVarNumerEmployees = document.getElementById( "{!$Component.block.blockSection.secondItem.acctNumEmployees}" ).value;
actionFunctionToUploadAll(jsVarRemoteAcctId,jsVarNumerEmployees);
}
</script>
<apex:actionFunction name="actionFunctionToUploadAll" action="{!apexMethodToUploadAll}" rerender="">
<apex:param name="paramRemoteAcctId" value=""/>
<apex:param name="paramNumberEmployees" value=""/>
</apex:actionFunction>
<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">           
<input type="text" id="remoteAcctId"/>       
</apex:pageBlockSectionItem>       
<apex:pageBlockSectionItem id="secondItem">           
<input type="text" id="acctNumEmployees"/>       
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem id="secondItem">          
<input type="button" value="Submit" onclick="jsMethodToUploadAll();"/>      
</apex:pageBlockSectionItem>  
</apex:pageBlockSection>  
</apex:pageBlock>  
</apex:page>


APEX CODE:
public void apexMethodToUploadAll(){
String vRemoteAcctId = Apexpages.currentPage().getParameters().get('paramRemoteAcctId');
String vNumberEmployees = Apexpages.currentPage().getParameters().get('paramNumberEmployees');
accountName = vRemoteAcctId;
account.name = accountName;
account.NumEmployees = vNumberEmployees; //Put the real field API name
update account;
}
 

Hopefully this answer your question.
Pablo.
 
 
This was selected as the best answer
Pablo_RoldanPablo_Roldan
Please click on Best Answer to make this question solved for anyone in the future if your problem has been solved.

Thanks,
Pablo.
MnZ321MnZ321
Sorry for being late. Thanks Pablo!
Swapnil Pal 1Swapnil Pal 1
Visual force Page:
<!-- 
    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;
    }
}