+ Start a Discussion
SS KarthickSS Karthick 

Account Details using Java Script Remoting

Hi folks, 
       Can anyone tell me how to display the account details using Visualforce Remoting

Thanks in advance
Best Answer chosen by SS Karthick
asish1989asish1989
Hi SS Karthik

Here is some code it will help you.

<apex:page controller="MyJSController">

    <script type="text/javascript">
   
          function getRemoteAccount() {
         
             var accountName = document.getElementById('acctSearch').value;
            
                  MyJSController.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.Phone;
                                                                               
                    } else if (event.type === 'exception') {
                          document.getElementById("responseErrors").innerHTML = event.message;
                    } else {
                          document.getElementById("responseErrors").innerHTML = event.message;
                        }
                   }, {escape:true});
             }
    </script>
   
    <input id="acctSearch" type="text"/>
    <button onclick="getRemoteAccount()">Get Account</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>

global with sharing class MyJSController {

     public String accountName { get; set; }
     public static Account account { get; set; }
    
     public MyJSController() { } // empty constructor
        @RemoteAction
        global static Account getAccount(String accountName) {
            account = [SELECT id, name, phone, type, BillingCity FROM Account WHERE name = :accountName];
                      
             return account;
       }
}