0

I have a commandButton, and I'd like to use the onclick event to show a list of Account records avaliable in my org. How can I do this without using a controller?

<apex:page>
    <script>
        function showAccounts() {
            alert('record is created successs');
        }
    </script>
    <apex:form>
        <apex:pageBlock>
            <apex:commandButton value="show Account" onclick="showAccounts();"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>
3
  • <apex:pageBlock> <apex:dataList value="{!act}" var="aname"> {!aname.name} </apex:dataList> </apex:pageBlock> please check , it may be help you; according to my understanding
    – Prade jo
    Commented Jul 22, 2016 at 10:24
  • i dnt want to use controller
    – Satya
    Commented Jul 22, 2016 at 10:25
  • You can use connection.js in your VF page you can able to retrieve the data from salesforce and also doing DML function via connection.js. So you'll learn connection.js. @sahisfdc Commented Jul 22, 2016 at 12:08

1 Answer 1

1

Use below code to get the list.

<apex:page >
    <script type="text/javascript">
    var __sfdcSessionId = '{!GETSESSIONID()}';
    </script>
    <script src="../../soap/ajax/37.0/connection.js"
          type="text/javascript"></script>
    <script type="text/javascript">     window.onload = setupPage;
    function setupPage() {
      //function contains all code to execute after page is rendered

      var state = { //state that you need when the callback is called
          output : document.getElementById("output"),
          startTime : new Date().getTime()};

      var callback = {
          //call layoutResult if the request is successful
          onSuccess: layoutResults,

          //call queryFailed if the api request fails
          onFailure: queryFailed,
          source: state};

      sforce.connection.query(
          "Select Id, Name, Industry From Account order by Industry",
           callback);
  }

  function queryFailed(error, source) {
    source.output.innerHTML = "An error has occurred: " + error;
  }

  /**
  * This method will be called when the toolkit receives a successful
  * response from the server.
  * @queryResult - result that server returned
  * @source - state passed into the query method call.
  */
  function layoutResults(queryResult, source) {
    if (queryResult.size > 0) {
      var output = "";

      //get the records array
      var records = queryResult.getArray('records');

      //loop through the records and construct html string
      for (var i = 0; i < records.length; i++) {
        var account = records[i];

        output += account.Id + " " + account.Name +
            " [Industry - " + account.Industry + "]<br>";
      }

    //render the generated html string
    source.output.innerHTML = output;
    }
  }
  </script>

    <div id="output"> </div>

</apex:page>
2
  • is it work for custom object , because when i am using your code with custom object, i am getting an error, that is An "error has occurred: ReferenceError: CrawleLink__c is not defined"
    – Prade jo
    Commented Jul 24, 2016 at 14:54
  • 1
    thanks @Prabhat kumar now it working for custom object also .. tahnks ,,,:)
    – Prade jo
    Commented Jul 24, 2016 at 15:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .