Find your content:

Search form

You are here

Javascript Remoting to display a table of records

 
Share

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


Attribution to: MnZ

Possible Suggestion/Solution #1

Here should be error:

result.NumberOfEmployees; // This exists in example you have copied not in your sObject

You don't have this field in custom object. Instead try any field which exists in custom object fields and query that field in SOQL also ex:

result.Name;

Just do this, replace alert(JSON.stringify(result)) with:

for(i=0;i<result.length;i++) {
     var name= result[i].Name; // Name is field api name
     alert(name)  ; // or do whatever you want with 
}

Attribution to: Ashwani
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/32748

My Block Status

My Block Content