1

Can I draw a Picklist in a Visualforce with a Javascript query?

With de ajax connection.js in visualforce, I'm trying to retrieve a list of some values, Ex:

var qr = sforce.connection.query(“SELECT Name FROM EmailTemplate WHERE Folder.DeveloperName ='”+ MyCustomTemplates +”‘”);

But I have no Idea how to draw the result in a Visualforce Picklist.

Any idea?

Thanks!!!

3
  • 2
    Why not just use a controller on your page?
    – Adrian Larson
    Commented Apr 27, 2016 at 16:30
  • 2
    You can add the select and option HTML elements to your page using JavaScript. But when you save the page there will be no automatic binding to properties on the controller. As Adrian suggests, building a list of SelectOption in a controller and using apex:selectList and apex:selectOptions will be simpler.
    – Keith C
    Commented Apr 27, 2016 at 17:03
  • Professional Edition :(
    – Raspi
    Commented Apr 28, 2016 at 8:21

1 Answer 1

2

You could create select within JavaScript

<apex:page >
<script type="text/javascript">
    var __sfdcSessionId = '{!GETSESSIONID()}';
</script>
<script src="../../soap/ajax/36.0/connection.js" type="text/javascript"/>
<script type="text/javascript">     window.onload = setupPage;
function setupPage() {
    var queryResult = sforce.connection.query("Select Id, Name From Account limit 5");
    var array = queryResult.getArray('records');

    var myDiv = document.getElementById("selectOption");
    var selectList = document.createElement("select");
    selectList.id = "mySelect";
    selectList.onchange = myFunction;
    myDiv.appendChild(selectList);

    for (var i = 0; i < array.length; i++) {
        var option = document.createElement("option");
        option.value = array[i].Id;
        option.text = array[i].Name;
        selectList.appendChild(option);
    }
}
function myFunction() {
    var x = document.getElementById("mySelect").value;
    document.getElementById("choice").innerHTML = "You selected: " + x;
}
</script>

<div id="selectOption"> </div>
<p id="choice"></p> 
</apex:page>
1
  • Perfect! Now I just need save with a commandButton action save the value. Thanks!
    – Raspi
    Commented Apr 28, 2016 at 15:29

You must log in to answer this question.

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