Toggle Side Panel

  • Home
  • Articles
    • All Articles
    • Blogs
    • Videos
    • Infographics
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce ConsultantsTop Salesforce Consultants
      • Marketing Cloud ConsultantsMarketing Cloud Consultants
      • Service Cloud ConsultantsService Cloud Consultants
      • Experience Cloud ConsultantsExperience Cloud Consultants
      • Analytics Cloud ConsultantsAnalytics Cloud Consultants
    • Salesforce Industry Expertise
      • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
      • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
      • Health Cloud ConsultantsHealth Cloud Consultants
      • Commerce Cloud ConsultantsCommerce Cloud Consultants
      • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
    • Salesforce Experts by Location
      • USATop Salesforce Consultants in USA
      • IndiaTop Salesforce Consultants in India
      • AustraliaTop Salesforce Consultants in Australia
      • United KingdomTop Salesforce Consultants in UK
      • CanadaTop Salesforce Consultants in Canada
  • Webinars
  • Marketplace
  • Advertise With Us
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Home
    • Articles
      • All Articles
      • Blogs
      • Videos
      • Infographics
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce ConsultantsTop Salesforce Consultants
        • Marketing Cloud ConsultantsMarketing Cloud Consultants
        • Service Cloud ConsultantsService Cloud Consultants
        • Experience Cloud ConsultantsExperience Cloud Consultants
        • Analytics Cloud ConsultantsAnalytics Cloud Consultants
      • Salesforce Industry Expertise
        • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
        • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
        • Health Cloud ConsultantsHealth Cloud Consultants
        • Commerce Cloud ConsultantsCommerce Cloud Consultants
        • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
      • Salesforce Experts by Location
        • USATop Salesforce Consultants in USA
        • IndiaTop Salesforce Consultants in India
        • AustraliaTop Salesforce Consultants in Australia
        • United KingdomTop Salesforce Consultants in UK
        • CanadaTop Salesforce Consultants in Canada
    • Webinars
    • Marketplace
    • Advertise With Us
    • Contact Us
    • Discussions
    Close search

    Activity › Forums › Salesforce® Discussions › How to take List of Account into a JavaScript variable on a Salesforce Visualforce page?

    Tagged: Account Billing Address, BillingState, Javascript in Salesforce, Javascript Variable, Salesforce Javascript Controller

    • Salesforce® Discussions

      How to take List of Account into a JavaScript variable on a Salesforce Visualforce page?

      Posted by shariq on August 14, 2017 at 9:55 AM

      I want to take Account billing address in a JavaScript array variable, how to do it?

      Ajay Prakash replied 5 years, 10 months ago 4 Members · 4 Replies
      • Account Billing Address
      • BillingState
      • Javascript in Salesforce
      • Javascript Variable
      • Salesforce Javascript Controller
    • 4 Replies
    • Radhakrishna

      Member
      August 14, 2017 at 10:22 AM

      Hello Shariq,

      JavaScript variable in Visualforce page

      A simple example :

      Visualforce :

      <apex:page controller="javascriptLearning">
      <script>
      alert('{!lstAssignToArray}');
      var myList = new Array();
      myList = '{!lstAssignToArray}';
      alert(myList);
      </script>
      </apex:page>

      Apex Controller:

      public with sharing class javascriptLearning {
      public List<String> lstAssignToArray {get;set;}

      public javascriptLearning()
      {
      lstAssignToArray = new List<String>();
      lstAssignToArray.add('ABC');
      lstAssignToArray.add('DEF');
      lstAssignToArray.add('GHI');
      lstAssignToArray.add('JKL');
      lstAssignToArray.add('MNO');
      lstAssignToArray.add('PQR');
      lstAssignToArray.add('STU');
      lstAssignToArray.add('VWX');
      lstAssignToArray.add('YZ');
      }
      }

      This works for me and in my alerts, I see that array has those values from the list!

    • shariq

      Member
      August 17, 2017 at 7:26 AM

      Hi Radhakrishna,

      Can you do this for Account List, Please give the example.

    • Parul

      Member
      September 15, 2018 at 3:14 PM

      You can take list of Account by using @RemoteAction:

      Apex Class

      public class AccountRemoteActionController {
      public String accountName { get; set; }
      public static List<Account> accounts { get; set; }

      public AccountRemoteActionController(){}

      @RemoteAction
      global static List<Account> getAccount(String accountName){
      String updatedAccName = '%'+accountName+'%';
      accounts = [select id, name, phone, type, numberofemployees from Account where name LIKE :updatedAccName];
      System.debug('Account Size : '+accounts.size());
      return accounts;
      }
      }

      VF Page

      <apex:page controller="AccountRemoteActionController">
      <script>
      function getAccountJS(){
      var accountNameJS = document.getElementById('accName').value;
      AccountRemoteActionController.getAccount(accountNameJS,
      function(result, event){
      if(event.status){
      for(var i=0; i < result.length; i++){
      document.getElementById("{!$Component.theBlock.thePageBlockSection.theFirstItem.accId}").innerHTML = result[i].Id;
      document.getElementById("{!$Component.theBlock.thePageBlockSection.theSecondItem.accNam}").innerHTML = result[i].Name;
      document.getElementById("{!$Component.theBlock.thePageBlockSection.theThirdItem.accPhone}").innerHTML = result[i].Phone;
      }
      }else if(event.type === 'exception'){
      document.getElementById("errors-js").innerHTML = event.message;
      }else{
      document.getElementById("errors-js").innerHTML = 'No Records Found..';
      }
      },{escape : true});
      }
      </script>
      Account Name : <input id="accName" type="text" />
      <button onclick="getAccountJS()">Get Account</button>
      <div id="errors-js"></div>

      <apex:pageBlock id="theBLock">
      <apex:pageBlockSection id="thePageBlockSection" columns="4">
      <apex:pageBlockSectionItem id="theFirstItem">
      <apex:outputText id="accId"/>
      </apex:pageBlockSectionItem>
      <apex:pageBlockSectionItem id="theSecondItem" >
      <apex:outputText id="accNam" />
      </apex:pageBlockSectionItem>
      <apex:pageBlockSectionItem id="theThirdItem" >
      <apex:outputText id="accPhone" />
      </apex:pageBlockSectionItem>
      </apex:pageBlockSection>
      </apex:pageBlock>
      </apex:page>

       

      Hope this will help you .

       

      Thanks

    • Ajay Prakash

      Member
      February 27, 2020 at 4:51 AM

      I found this interesting topic today. Using remote action will open a different thread. I believe, The following approach will help -

      In your javascript code parse the get set variable in the following way-

      var accListTemp='{!accountList}';
      var accList = JSON.parse(accListTemp);

      Here in accList you will get a list of objects in javascript. You will get a JSON parsing error if you use the get set variable as a list of sobject. It will be better if you return a JSON from apex code and parse in Javascript.

      public list<Account> getaccountList {get;set;}
      public String accountList {get;set;}
      public getaccountList(){
         getaccountList = [Select Id, Name from Account];
         accountList = JSON.serialize(getaccountList);
      }

      Thanks.

       

    Log In to reply.

    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me

    application solution

    Popular Salesforce Blogs

    Apex Test Class in Salesforce - Lear All About It

    Blog in Salesforce Apex

    Introduction Apex testing framework enables you to write and execute tests for your apex classes and triggers on the lightning platform.  Apex unit tests ensure…

    Apex Rationale, Apex Test Class, Apex Test Class in Salesforce, Apex Testing Framework, Apex Unit Test
    NARENDRA Oct 17, 2022
    2,099  Views

    Pardot Audit: How and Why It is Essential to Conduct One Today | Salesforce

    Blog in Salesforce Products

    Pardot is a powerful B2B marketing tool that allows marketers to run and manage multiple campaigns with ease. It is capable of automating processes and driving…

    Automating Processes, B2B, B2B Marketing Tool, Best Practices, Bulk Data
    DemandBlue Dec 17, 2020
    3,461  Views
    Providing Login Access Into The Salesforce Org With Limited Access To Salesforce AppExchange App Provider

    Granting Log-in Access Into Your Salesforce Org With Limited Access To Salesforce AppExchange App Provider

    Blog in Others

    Hi folks, Today we will learn, how to give grant access to Salesforce AppExchange App provider? There are many people who don't know how to…

    Lightning View, Salesforce AppExchange, Salesforce Lightning, Salesforce Lightning View User, Salesforce Org
    Prafull Oct 21, 2019
    6,046  Views
    Footer Forcetalks logo

    support@forcetalks.com

    • twitterx

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2023

    Top Salesforce® Bloggers 2023

    Top Salesforce Consultants

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    InsightHub

    Salesforce Blogs

    Salesforce Videos

    Salesforce Groups

    Salesforce Jobs

    © 2026 - Forcetalks ● All Rights Reserved

    Salesforce® is a trademark of Salesforce® Inc. No claim is made to the exclusive right to use “Salesforce”. Any services offered within the Forcetalks website/app are not sponsored or endorsed by Salesforce®.

    Try AuditMyCRM - It is a Salesforce CRM Audit tool which comprehensively scans your Salesforce org and gives you the list of errors or warnings you need to take care of.
    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.